Class: AIXM::D
Overview
Dimension, 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
-
#dim ⇒ Object
Dimension.
-
#unit ⇒ Object
Unit.
Instance Method Summary collapse
- #<=>(other) ⇒ Object
- #==(other) ⇒ Object
-
#initialize(dim, unit) ⇒ D
constructor
See the overview for examples.
- #inspect ⇒ String
-
#to_ft ⇒ AIXM::D
Convert dimension.
-
#to_km ⇒ AIXM::D
Convert dimension.
-
#to_m ⇒ AIXM::D
Convert dimension.
-
#to_nm ⇒ AIXM::D
Convert dimension.
-
#to_s ⇒ String
Human readable representation (e.g. “123.0 m”).
-
#zero? ⇒ Boolean
Whether dimension is zero.
Methods included from Concerns::HashEquality
Constructor Details
Instance Attribute Details
#dim ⇒ Float #dim=(value) ⇒ Object
Dimension
33 34 35 |
# File 'lib/aixm/d.rb', line 33 def dim @dim end |
#unit ⇒ Symbol #unit=(value) ⇒ Object
Unit
41 42 43 |
# File 'lib/aixm/d.rb', line 41 def unit @unit end |
Instance Method Details
#<=>(other) ⇒ Object
84 85 86 |
# File 'lib/aixm/d.rb', line 84 def <=>(other) dim <=> other.send(:"to_#{unit}").dim end |
#==(other) ⇒ Object
89 90 91 |
# File 'lib/aixm/d.rb', line 89 def ==(other) self.class === other && (self <=> other).zero? end |
#inspect ⇒ String
49 50 51 |
# File 'lib/aixm/d.rb', line 49 def inspect %Q(#<#{self.class} #{to_s}>) end |
#to_ft ⇒ AIXM::D
Convert dimension
76 77 78 79 80 81 |
# File 'lib/aixm/d.rb', line 76 UNITS.each_key do |target_unit| define_method "to_#{target_unit}" do return self if unit == target_unit self.class.new((dim * UNITS[unit][target_unit]).round(8), target_unit) end end |
#to_km ⇒ AIXM::D
Convert dimension
76 77 78 79 80 81 |
# File 'lib/aixm/d.rb', line 76 UNITS.each_key do |target_unit| define_method "to_#{target_unit}" do return self if unit == target_unit self.class.new((dim * UNITS[unit][target_unit]).round(8), target_unit) end end |
#to_m ⇒ AIXM::D
Convert dimension
76 77 78 79 80 81 |
# File 'lib/aixm/d.rb', line 76 UNITS.each_key do |target_unit| define_method "to_#{target_unit}" do return self if unit == target_unit self.class.new((dim * UNITS[unit][target_unit]).round(8), target_unit) end end |
#to_nm ⇒ AIXM::D
Convert dimension
76 77 78 79 80 81 |
# File 'lib/aixm/d.rb', line 76 UNITS.each_key do |target_unit| define_method "to_#{target_unit}" do return self if unit == target_unit self.class.new((dim * UNITS[unit][target_unit]).round(8), target_unit) end end |
#to_s ⇒ String
Returns human readable representation (e.g. “123.0 m”).
54 55 56 |
# File 'lib/aixm/d.rb', line 54 def to_s [dim, unit].join(' '.freeze) end |
#zero? ⇒ Boolean
Whether dimension is zero.
25 |
# File 'lib/aixm/d.rb', line 25 def_delegator :@dim, :zero? |