Class: Temperature

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

Overview

Temperature type with unit convertion methods

The Temperature class accepts any temperature value in degrees celsius and can convert it to any other unit with helper methods

Author:

Constant Summary collapse

KELVIN_SHIFT =
273.15
FAHRENHEIT_OFFSET =
32
FAHRENHEIT_SLOPE =
Rational(9,5)

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(temperature) ⇒ Temperature

Returns a new instance of Temperature.

Parameters:

  • temperature (Number)

    temperature value in degrees celsius



16
17
18
# File 'lib/temperature.rb', line 16

def initialize temperature
    @temperature = temperature
end

Instance Attribute Details

#temperatureObject (readonly)

Returns the value of attribute temperature.



9
10
11
# File 'lib/temperature.rb', line 9

def temperature
  @temperature
end

Instance Method Details

#to_celsiusNumber

Return the temperature value in degrees celsius

Examples:

temperature = Temperature.new 12.3
puts temperature.to_celsius

Returns:

  • (Number)

    temperature in degrees celsius



25
26
27
# File 'lib/temperature.rb', line 25

def to_celsius
    temperature
end

#to_fahrenheitObject



29
30
31
# File 'lib/temperature.rb', line 29

def to_fahrenheit
    (to_celsius * FAHRENHEIT_SLOPE) + FAHRENHEIT_OFFSET
end

#to_kelvinObject



33
34
35
# File 'lib/temperature.rb', line 33

def to_kelvin
    to_celsius + KELVIN_SHIFT
end