Sha256: 802fd7c518626f845cf75e63a864f917b4f1a9ca9159c73295603c0400213ec7
Contents?: true
Size: 1.76 KB
Versions: 2
Compression:
Stored size: 1.76 KB
Contents
# coding: utf-8 module Valuevaluevalue class Mass attr_accessor :value, :unit UNITS = [ MassUnit.new('Atomic Mass Unit', 'amu', 1.6605402e-27), MassUnit.new('Carat', 'ct', 0.0002), MassUnit.new('Cental', 'cwt', 45.359237), MassUnit.new('Centigram', 'cg', 0.00001), MassUnit.new('Decagram', 'dkg', 0.01), MassUnit.new('Dram', 'dr', 0.001771845195312), MassUnit.new('Grain', 'gr', 0.00006479891), MassUnit.new('Gram', 'g', 0.001), MassUnit.new('Hundredweight', 'hwt', 50.80234544), MassUnit.new('Kilogram', 'kg', 1), MassUnit.new('Long Ton', 'lton', 1016.0469088), MassUnit.new('Microgram', 'mcg', 1E-9), MassUnit.new('Milligram', 'mg', 0.000001), MassUnit.new('Ounce', 'oz', 0.028349523125), MassUnit.new('Pennyweight', 'dwt', 0.00155517384), MassUnit.new('Pound', 'lbs', 0.45359237), MassUnit.new('Quarter', 'qt', 12.70058636), MassUnit.new('Short Ton', 'ton', 907.18474), MassUnit.new('Stone', 'st', 6.35029318), MassUnit.new('Tonne', 't', 1000), MassUnit.new('Troy Ounce', 'troz', 0.0311034768) ].freeze def initialize(value, unit) unit_exists?(unit) @value = value @unit = unit end def convert_to(unit) unit_exists?(unit) convert_to_kg / find_unit(unit).mass_in_kg end private def unit_exists?(unit) UNITS.include?(unit) || UNITS.map(&:name).include?(unit.to_s) || UNITS.map(&:abbreviation).include?(unit.to_s) end 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 def convert_to_kg value * unit.mass_in_kg end end end
Version data entries
2 entries across 2 versions & 1 rubygems
Version | Path |
---|---|
valuevaluevalue-0.1.1 | lib/valuevaluevalue/mass.rb |
valuevaluevalue-0.1.0 | lib/valuevaluevalue/mass.rb |