Class: Scalar::SupportClasses::ConversionTable

Inherits:
Object
  • Object
show all
Defined in:
lib/scalar/support_classes/conversion_table.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(base_unit:) ⇒ ConversionTable

Returns a new instance of ConversionTable.



4
5
6
7
8
# File 'lib/scalar/support_classes/conversion_table.rb', line 4

def initialize(base_unit:)
  self.table = {}
  self.base_unit = base_unit
  add(name: base_unit, base_unit_per: 1.to_r)
end

Instance Attribute Details

#base_unitObject

Returns the value of attribute base_unit.



25
26
27
# File 'lib/scalar/support_classes/conversion_table.rb', line 25

def base_unit
  @base_unit
end

#tableObject

Returns the value of attribute table.



25
26
27
# File 'lib/scalar/support_classes/conversion_table.rb', line 25

def table
  @table
end

Instance Method Details

#add(name:, base_unit_per:) ⇒ Object



10
11
12
13
14
15
16
17
18
19
# File 'lib/scalar/support_classes/conversion_table.rb', line 10

def add(name:, base_unit_per:)
  raise unless name.is_a? Symbol

  table[name] = {name => 1.to_r}

  table.keys.each do |k|
    table[k][name] = (table[k][base_unit] / base_unit_per).to_r
    table[name][k] = 1.to_r / table[k][name]
  end
end

#call(from:, to:) ⇒ Object



21
22
23
# File 'lib/scalar/support_classes/conversion_table.rb', line 21

def call(from:, to:)
  table[from][to]
end