Class: Converter

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

Overview

Convert temperature in fahrenheit or kelvin to degrees celcius

Examples:

converter = Converter.new
args = { unit: 'fahrenheit', value: 1.8 }
@range = converter.range_to_celcius(args) / 2.0
args[:value] = 21
@wanted_value = converter.temp_to_celcius(args)

Instance Method Summary collapse

Instance Method Details

#range_to_celcius(args) ⇒ Object

If range not in celcius, it will be converted into celcius

Parameters:

  • args (Hash)

Options Hash (args):

  • :value (Numeric)

    the given range

  • :unit (String)

    the unit of the range



15
16
17
18
19
20
21
# File 'lib/converter.rb', line 15

def range_to_celcius(args)
  tempo = if args[:unit] == 'fahrenheit'
            args[:value] / 1.8
          else
            args[:value]
          end
end

#temp_fahrenheit(value) ⇒ Numeric

Convert value from fahrenheit to celcius

Parameters:

  • value (Numeric)

    the value that should be converted

Returns:

  • (Numeric)

    return the temperature in celcius



42
43
44
# File 'lib/converter.rb', line 42

def temp_fahrenheit(value)
  (value - 32) / 1.8
end

#temp_kelvin(value) ⇒ Numeric

Convert value from kelvin to celcius

Parameters:

  • value (Numeric)

    the value that should be converted

Returns:

  • (Numeric)

    return the temperature in celcius



50
51
52
# File 'lib/converter.rb', line 50

def temp_kelvin(value)
  value - 273.15
end

#temp_to_celcius(args) ⇒ Object

If temperature is not in celcius, it will be converted into celcius

Parameters:

  • args (Hash)

Options Hash (args):

  • :value (Numeric)

    the given temperature

  • :unit (String)

    the unit of the temperature



28
29
30
31
32
33
34
35
36
# File 'lib/converter.rb', line 28

def temp_to_celcius(args)
  tempo = if args[:unit] == 'fahrenheit'
            temp_fahrenheit(args[:value])
          elsif args[:unit] == 'kelvin'
            temp_kelvin(args[:value])
          else
            args[:value]
          end
end