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

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

#unitSymbol

Returns unit (see UNITS).

Returns:

  • (Symbol)

    unit (see UNITS)


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

def unit
  @unit
end

#wgtFloat

Returns weight.

Returns:

  • (Float)

    weight


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

def wgt
  @wgt
end

Instance Method Details

#<=>(other) ⇒ Integer

Returns:

  • (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?

Returns:

  • (Boolean)

See Also:

  • Object#==

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

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

#hashInteger

Returns:

  • (Integer)

See Also:

  • Object#hash

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

def hash
  to_s.hash
end

#inspectString

Returns:

  • (String)

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

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

#to_kgAIXM::W

Returns convert weight.

Returns:


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

Returns convert weight.

Returns:


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

Returns human readable representation (e.g. “123 t”).

Returns:

  • (String)

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

Returns convert weight.

Returns:


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

Returns convert weight.

Returns:


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.

Returns:

  • (Boolean)

    whether weight is zero


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

def_delegator :@wgt, :zero?