class Valuevaluevalue::Mass

Constants

UNITS

Attributes

unit[RW]
value[RW]

Public Class Methods

new(value, unit) click to toggle source
# File lib/valuevaluevalue/mass.rb, line 31
def initialize(value, unit)
  unit_exists?(unit)
  @value = value
  @unit  = unit
end

Public Instance Methods

convert_to(unit) click to toggle source
# File lib/valuevaluevalue/mass.rb, line 37
def convert_to(unit)
  unit_exists?(unit)
  convert_to_kg / find_unit(unit).mass_in_kg
end

Private Instance Methods

convert_to_kg() click to toggle source
# File lib/valuevaluevalue/mass.rb, line 56
def convert_to_kg
  value * unit.mass_in_kg
end
find_unit(unit) click to toggle source
# File lib/valuevaluevalue/mass.rb, line 50
def find_unit(unit)
  raise 'Unit does not exist' unless unit_exists?(unit)
  unit = unit.to_s
  UNITS.select { |u| u.name == unit || u.abbreviation == unit }.first
end
unit_exists?(unit) click to toggle source
# File lib/valuevaluevalue/mass.rb, line 44
def unit_exists?(unit)
  UNITS.include?(unit) ||
    UNITS.map(&:name).include?(unit.to_s) ||
    UNITS.map(&:abbreviation).include?(unit.to_s)
end