Class: AIXM::W
Overview
Weight
Constant Summary collapse
- UNITS =
{ kg: { t: 0.001, lb: 2.204622622, ton: 0.00110231131 }, t: { kg: 1000, lb: 2204.622622, ton: 1.10231131 }, lb: { kg: 0.45359237, t: 0.00045359237, ton: 0.000499999999581 }, ton: { kg: 907.18474, t: 0.90718474, lb: 2000.00000013718828 } }.freeze
Instance Attribute Summary collapse
-
#unit ⇒ Object
Unit.
-
#wgt ⇒ Object
Weight.
Instance Method Summary collapse
- #<=>(other) ⇒ Object
- #==(other) ⇒ Object
-
#initialize(wgt, unit) ⇒ W
constructor
See the overview for examples.
- #inspect ⇒ String
-
#to_kg ⇒ AIXM::W
Convert weight.
-
#to_lb ⇒ AIXM::W
Convert weight.
-
#to_s ⇒ String
Human readable representation (e.g. “123 t”).
-
#to_t ⇒ AIXM::W
Convert weight.
-
#to_ton ⇒ AIXM::W
Convert weight.
-
#zero? ⇒ Boolean
Whether weight is zero.
Methods included from Concerns::HashEquality
Constructor Details
Instance Attribute Details
#unit ⇒ Float #unit=(value) ⇒ Object
Unit
41 42 43 |
# File 'lib/aixm/w.rb', line 41 def unit @unit end |
Instance Method Details
#<=>(other) ⇒ Object
84 85 86 |
# File 'lib/aixm/w.rb', line 84 def <=>(other) wgt <=> other.send(:"to_#{unit}").wgt end |
#==(other) ⇒ Object
89 90 91 |
# File 'lib/aixm/w.rb', line 89 def ==(other) self.class === other && (self <=> other).zero? end |
#inspect ⇒ String
49 50 51 |
# File 'lib/aixm/w.rb', line 49 def inspect %Q(#<#{self.class} #{to_s}>) end |
#to_kg ⇒ AIXM::W
Convert weight
76 77 78 79 80 81 |
# File 'lib/aixm/w.rb', line 76 UNITS.each_key do |target_unit| define_method "to_#{target_unit}" do return self if unit == target_unit self.class.new((wgt * UNITS[unit][target_unit]).round(8), target_unit) end end |
#to_lb ⇒ AIXM::W
Convert weight
76 77 78 79 80 81 |
# File 'lib/aixm/w.rb', line 76 UNITS.each_key do |target_unit| define_method "to_#{target_unit}" do return self if unit == target_unit self.class.new((wgt * UNITS[unit][target_unit]).round(8), target_unit) end end |
#to_s ⇒ String
Returns human readable representation (e.g. “123 t”).
54 55 56 |
# File 'lib/aixm/w.rb', line 54 def to_s [wgt, unit].join(' '.freeze) end |
#to_t ⇒ AIXM::W
Convert weight
76 77 78 79 80 81 |
# File 'lib/aixm/w.rb', line 76 UNITS.each_key do |target_unit| define_method "to_#{target_unit}" do return self if unit == target_unit self.class.new((wgt * UNITS[unit][target_unit]).round(8), target_unit) end end |
#to_ton ⇒ AIXM::W
Convert weight
76 77 78 79 80 81 |
# File 'lib/aixm/w.rb', line 76 UNITS.each_key do |target_unit| define_method "to_#{target_unit}" do return self if unit == target_unit self.class.new((wgt * UNITS[unit][target_unit]).round(8), target_unit) end end |
#zero? ⇒ Boolean
Whether weight is zero.
25 |
# File 'lib/aixm/w.rb', line 25 def_delegator :@wgt, :zero? |