Class: AIXM::W
- Inherits:
-
Object
- Object
- AIXM::W
- Extended by:
- Forwardable
- Includes:
- Comparable
- Defined in:
- lib/aixm/w.rb
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 ⇒ Symbol
Unit (see UNITS).
-
#wgt ⇒ Float
Weight.
Instance Method Summary collapse
- #<=>(other) ⇒ Integer
- #==(other) ⇒ Boolean (also: #eql?)
- #hash ⇒ Integer
-
#initialize(wgt, unit) ⇒ W
constructor
A new instance of W.
- #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.
Constructor Details
#initialize(wgt, unit) ⇒ W
Returns a new instance of W.
30 31 32 |
# File 'lib/aixm/w.rb', line 30 def initialize(wgt, unit) self.wgt, self.unit = wgt, unit end |
Instance Attribute Details
#unit ⇒ Symbol
Returns unit (see UNITS).
28 29 30 |
# File 'lib/aixm/w.rb', line 28 def unit @unit end |
#wgt ⇒ Float
Returns weight.
25 26 27 |
# File 'lib/aixm/w.rb', line 25 def wgt @wgt end |
Instance Method Details
#<=>(other) ⇒ Integer
69 70 71 |
# File 'lib/aixm/w.rb', line 69 def <=>(other) wgt <=> other.send(:"to_#{unit}").wgt end |
#==(other) ⇒ Boolean Also known as: eql?
75 76 77 |
# File 'lib/aixm/w.rb', line 75 def ==(other) self.class === other && (self <=> other).zero? end |
#hash ⇒ Integer
82 83 84 |
# File 'lib/aixm/w.rb', line 82 def hash to_s.hash end |
#inspect ⇒ String
35 36 37 |
# File 'lib/aixm/w.rb', line 35 def inspect %Q(#<#{self.class} #{to_s}>) end |
#to_kg ⇒ AIXM::W
Returns convert weight.
60 61 62 63 64 65 |
# File 'lib/aixm/w.rb', line 60 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
Returns convert weight.
60 61 62 63 64 65 |
# File 'lib/aixm/w.rb', line 60 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”).
40 41 42 |
# File 'lib/aixm/w.rb', line 40 def to_s [wgt, unit].join(' ') end |
#to_t ⇒ AIXM::W
Returns convert weight.
60 61 62 63 64 65 |
# File 'lib/aixm/w.rb', line 60 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
Returns convert weight.
60 61 62 63 64 65 |
# File 'lib/aixm/w.rb', line 60 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
Returns whether weight is zero.
22 |
# File 'lib/aixm/w.rb', line 22 def_delegator :@wgt, :zero? |