## Example ## unit = Unit.km #=> # unit.name #=> 'kilometre' unit.symbol #=> 'kg' unit.dimensions #=> # unit.measures #=> 'length' unit.alternatives :name #=> ['metre', 'megametre', 'gigametre', 'terametre', 'angstrom', # 'astronomical unit', 'baromil', 'chain', 'dram', 'ell', 'fathom', # 'fermi', 'foot us survey', 'foot', 'furlong', 'hand', 'inch', # 'nautical league', 'statute league', 'light year', 'line', # 'link', 'yard'] other_unit = Unit.hour other_unit.name #=> 'hour' other_unit.symbol #=> 'h' other_unit.measures #=> 'time' other_unit.alternatives :symbol #=> [ 's', 'ks', 'Ms', 'Gs', 'Ts', 'd', 'min' ] another_unit = unit / other_unit #=> # another_unit.name #=> 'kilometer per hour' another_unit.symbol #=> 'km h^-1' another_unit.measures #=> 'velocity' last_unit = Unit.m last.unit.measures #=> 'length' square = last_unit ** 2 #=> # square.symbol #=> 'm^2' square.measures #=> 'area' ---- # Define quantity - method 1 quantity = Quantity.new(1234.5678, :lb) #=> # quantity.value #=> 1234.5678 quantity.unit #=> # quantity.unit.symbol #=> 'lb' # Define quantity - method 2 string = quantity.to_s #=> "1234.5678 lb" quantity = Quantity.parse(string) #=> # quantity.to_s #=> "1234.5678 lb" # Define quantity - method 3 quantity = 1234.5678.lb #=> # quantity.to_s #=> "1234.5678 lb" # Multiply by scalar new_quantity = quantity * 4 #=> # new_quantity.to_s #=> "4938.2712 lb" # Convert unit converted_quantity = new_quantity.to_kg #=> # converted_quantity.to_s #=> "2239.96213731074 kg" ---- # One line conversion of a quantity to a new unit # 5000 litres into US petroleum barrels 5000.L.to_bbl.value #=> 31.4490528488754 ---- # Complex example # Define energy units kW = Unit.kW #=> # kW.name #=> 'kilowatt' kW.measures #=> 'power' h = Unit.h #=> # h.symbol #=> 'h' h.measures #=> 'time' # Create compound unit for energy kWh = kW / h #=> # kWh.name #=> 'kilowatt hour' kWh.measures #=> 'energy' kWh.alternatives :symbol #=> ['J', 'kJ', 'MJ', 'GJ', 'TJ', 'BTU', 'cal', 'CHU', # 'dyn cm', 'eV', 'erg', 'Eh'] kWh.alternative :name #=> ['joule', 'kilojoule', 'megajoule', 'gigajoule', # 'terajoule', 'british thermal unit', 'calorie', # 'celsius heat unit', 'dyne centimetre', 'electron volt', # 'erg', 'hartree'] # Create mass unit kg = Unit.kg #=> # # Create emission factor compound unit kg_per_kWh = kg / kWh #=> # kg_per_kWh.symbol #=> "kg_kW^-1_h^-1 kg_per_kWh.name #=> 'kilogram per kilowatt hour' # Create emission factor quantity emission_factor = Quantity.new(0.54, kg_per_kWh) #=> # emission_factor.to_s #=> "0.54 kg kW^-1 h^-1" # Create consumption quantity consumption = Quantity.new(9885.5, kWh) #=> # consumption.to_s #=> "9885.5 kWh" # Calculate emissions emissions = consumption * emission_factor #=> # emissions.to_s #=> "5338.17 kg"