Class: Measured::ConversionTableBuilder
- Inherits:
-
Object
- Object
- Measured::ConversionTableBuilder
- Defined in:
- lib/measured/conversion_table_builder.rb
Instance Attribute Summary collapse
-
#units ⇒ Object
readonly
Returns the value of attribute units.
Instance Method Summary collapse
-
#initialize(units) ⇒ ConversionTableBuilder
constructor
A new instance of ConversionTableBuilder.
- #to_h ⇒ Object
Constructor Details
#initialize(units) ⇒ ConversionTableBuilder
Returns a new instance of ConversionTableBuilder.
4 5 6 |
# File 'lib/measured/conversion_table_builder.rb', line 4 def initialize(units) @units = units end |
Instance Attribute Details
#units ⇒ Object (readonly)
Returns the value of attribute units.
2 3 4 |
# File 'lib/measured/conversion_table_builder.rb', line 2 def units @units end |
Instance Method Details
#to_h ⇒ Object
8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 |
# File 'lib/measured/conversion_table_builder.rb', line 8 def to_h table = {} units.map{|u| u.name}.each do |to_unit| to_table = {to_unit => Rational(1, 1)} table.each do |from_unit, from_table| conversion = find_conversion(to: from_unit, from: to_unit) to_table[from_unit] = conversion from_table[to_unit] = 1 / conversion end table[to_unit] = to_table end table end |