Class: AIXM::W

Inherits:
Object show all
Extended by:
Forwardable
Includes:
Concerns::HashEquality, 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

Methods included from Concerns::HashEquality

#eql?, #hash

Constructor Details

#initialize(wgt, unit) ⇒ W

See the overview for examples.



44
45
46
# File 'lib/aixm/w.rb', line 44

def initialize(wgt, unit)
  self.wgt, self.unit = wgt, unit
end

Instance Attribute Details

#unitFloat #unit=(value) ⇒ Object

Unit

Overloads:

  • #unitFloat

    Returns any of UNITS.

    Returns:

    • (Float)

      any of UNITS

  • #unit=(value) ⇒ Object

    Parameters:

    • value (Float)

      any of UNITS



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

def unit
  @unit
end

#wgtFloat #wgt=(value) ⇒ Object

Weight

Overloads:

  • #wgtFloat

    Returns:

    • (Float)
  • #wgt=(value) ⇒ Object

    Parameters:

    • value (Float)


33
34
35
# File 'lib/aixm/w.rb', line 33

def wgt
  @wgt
end

Instance Method Details

#<=>(other) ⇒ Object

See Also:

  • Object#<=>


84
85
86
# File 'lib/aixm/w.rb', line 84

def <=>(other)
  wgt <=> other.send(:"to_#{unit}").wgt
end

#==(other) ⇒ Object

See Also:

  • Object#==


89
90
91
# File 'lib/aixm/w.rb', line 89

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

#inspectString

Returns:

  • (String)


49
50
51
# File 'lib/aixm/w.rb', line 49

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

#to_kgAIXM::W

Convert weight

Returns:



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

Convert weight

Returns:



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_sString

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

Returns:

  • (String)

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

Convert weight

Returns:



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

Convert weight

Returns:



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.

Returns:

  • (Boolean)


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

def_delegator :@wgt, :zero?