Class: AIXM::W

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

Overview

Weight

Examples:

AIXM.w(2.9, :t)

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

Instance Method Summary collapse

Constructor Details

#initialize(wgt, unit) ⇒ 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

#unitSymbol


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

def unit
  @unit
end

#wgtFloat


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

def wgt
  @wgt
end

Instance Method Details

#<=>(other) ⇒ Integer

See Also:

  • Object#<=>

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?

See Also:

  • Object#==

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

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

#hashInteger

See Also:

  • Object#hash

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

def hash
  to_s.hash
end

#inspectString


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

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

#to_kgAIXM::W


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_lbAIXM::W


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_sString


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

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

#to_tAIXM::W


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_tonAIXM::W


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


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

def_delegator :@wgt, :zero?