Class: Convertio::Converter

Inherits:
Object
  • Object
show all
Defined in:
lib/convertio.rb

Overview

This class validates input and performs conversion

Instance Method Summary collapse

Constructor Details

#initialize(value, from, to) ⇒ Converter

Returns a new instance of Converter.



95
96
97
98
99
100
101
102
# File 'lib/convertio.rb', line 95

def initialize(value, from, to)
  @value = value
  @from = from
  @to = to
  @from_unit = UNITS[@from]
  @to_unit = UNITS[@to]
  validate!
end

Instance Method Details

#convertObject



104
105
106
# File 'lib/convertio.rb', line 104

def convert
  (@value * @from_unit[:per_base]) / @to_unit[:per_base].to_f
end