Class: AIXM::D

Inherits:
Object
  • Object
show all
Extended by:
Forwardable
Includes:
Comparable
Defined in:
lib/aixm/d.rb

Overview

Distance or length

Examples:

AIXM.d(123, :m)

Constant Summary collapse

UNITS =
{
  ft: { km: 0.0003048, m: 0.3048, nm: 0.000164578833554 },
  km: { ft: 3280.839895, m: 1000, nm: 0.539956803 },
  m: { ft: 3.280839895, km: 0.001, nm: 0.000539956803 },
  nm: { ft: 6076.11548554, km: 1.852, m: 1852 }
}.freeze

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(dist, unit) ⇒ D


30
31
32
# File 'lib/aixm/d.rb', line 30

def initialize(dist, unit)
  self.dist, self.unit = dist, unit
end

Instance Attribute Details

#distFloat


25
26
27
# File 'lib/aixm/d.rb', line 25

def dist
  @dist
end

#unitSymbol


28
29
30
# File 'lib/aixm/d.rb', line 28

def unit
  @unit
end

Instance Method Details

#<=>(other) ⇒ Integer

See Also:

  • Object#<=>

69
70
71
# File 'lib/aixm/d.rb', line 69

def <=>(other)
  dist <=> other.send(:"to_#{unit}").dist
end

#==(other) ⇒ Boolean Also known as: eql?

See Also:

  • Object#==

75
76
77
# File 'lib/aixm/d.rb', line 75

def ==(other)
  self.class === other  && (self <=> other).zero?
end

#hashInteger

See Also:

  • Object#hash

82
83
84
# File 'lib/aixm/d.rb', line 82

def hash
  to_s.hash
end

#inspectString


35
36
37
# File 'lib/aixm/d.rb', line 35

def inspect
  %Q(#<#{self.class} #{to_s}>)
end

#to_ftAIXM::D


60
61
62
63
64
65
# File 'lib/aixm/d.rb', line 60

UNITS.each_key do |target_unit|
  define_method "to_#{target_unit}" do
    return self if unit == target_unit
    self.class.new((dist * UNITS[unit][target_unit]).round(8), target_unit)
  end
end

#to_kmAIXM::D


60
61
62
63
64
65
# File 'lib/aixm/d.rb', line 60

UNITS.each_key do |target_unit|
  define_method "to_#{target_unit}" do
    return self if unit == target_unit
    self.class.new((dist * UNITS[unit][target_unit]).round(8), target_unit)
  end
end

#to_mAIXM::D


60
61
62
63
64
65
# File 'lib/aixm/d.rb', line 60

UNITS.each_key do |target_unit|
  define_method "to_#{target_unit}" do
    return self if unit == target_unit
    self.class.new((dist * UNITS[unit][target_unit]).round(8), target_unit)
  end
end

#to_nmAIXM::D


60
61
62
63
64
65
# File 'lib/aixm/d.rb', line 60

UNITS.each_key do |target_unit|
  define_method "to_#{target_unit}" do
    return self if unit == target_unit
    self.class.new((dist * UNITS[unit][target_unit]).round(8), target_unit)
  end
end

#to_sString


40
41
42
# File 'lib/aixm/d.rb', line 40

def to_s
  [dist, unit].join(' ')
end

#zero?Boolean


22
# File 'lib/aixm/d.rb', line 22

def_delegator :@dist, :zero?