Class: AIXM::D
- Extended by:
- Forwardable
- Includes:
- Comparable
- Defined in:
- lib/aixm/d.rb
Overview
Distance or length
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
-
#dist ⇒ Float
Distance.
-
#unit ⇒ Symbol
Unit (see UNITS).
Instance Method Summary collapse
- #<=>(other) ⇒ Integer
- #==(other) ⇒ Boolean (also: #eql?)
- #hash ⇒ Integer
-
#initialize(dist, unit) ⇒ D
constructor
A new instance of D.
- #inspect ⇒ String
-
#to_ft ⇒ AIXM::D
Convert distance.
-
#to_km ⇒ AIXM::D
Convert distance.
-
#to_m ⇒ AIXM::D
Convert distance.
-
#to_nm ⇒ AIXM::D
Convert distance.
-
#to_s ⇒ String
Human readable representation (e.g. “123 m”).
-
#zero? ⇒ Boolean
Whether distance is zero.
Constructor Details
#initialize(dist, unit) ⇒ D
Returns a new instance of 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
#dist ⇒ Float
Returns distance.
25 26 27 |
# File 'lib/aixm/d.rb', line 25 def dist @dist end |
#unit ⇒ Symbol
Returns unit (see UNITS).
28 29 30 |
# File 'lib/aixm/d.rb', line 28 def unit @unit end |
Instance Method Details
#<=>(other) ⇒ Integer
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?
75 76 77 |
# File 'lib/aixm/d.rb', line 75 def ==(other) self.class === other && (self <=> other).zero? end |
#hash ⇒ Integer
82 83 84 |
# File 'lib/aixm/d.rb', line 82 def hash to_s.hash end |
#inspect ⇒ String
35 36 37 |
# File 'lib/aixm/d.rb', line 35 def inspect %Q(#<#{self.class} #{to_s}>) end |
#to_ft ⇒ AIXM::D
Returns convert distance.
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_km ⇒ AIXM::D
Returns convert distance.
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_m ⇒ AIXM::D
Returns convert distance.
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_nm ⇒ AIXM::D
Returns convert distance.
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_s ⇒ String
Returns human readable representation (e.g. “123 m”).
40 41 42 |
# File 'lib/aixm/d.rb', line 40 def to_s [dist, unit].join(' ') end |
#zero? ⇒ Boolean
Returns whether distance is zero.
22 |
# File 'lib/aixm/d.rb', line 22 def_delegator :@dist, :zero? |