class Temperatures

Constants

KELVINOFFSET
OFFSETFAHRENHEIT
SLOPEFAHRENHEIT

Attributes

kelvin[RW]

Public Class Methods

new(value = 0) click to toggle source

Constructor that it passes by parameter the hash value to know what type of temperature we have selected in the line argument

# File lib/temperatures.rb, line 8
def initialize(value = 0)
  if value[:celsius]
    self.celsius = value[:celsius]
  elsif value[:fahrenheit]
    self.fahrenheit = value[:fahrenheit]
  elsif value[:kelvin]
    self.kelvin = value[:kelvin]
  else
    raise "No temperature unit given in temperature hash"
  end
end

Public Instance Methods

celsius=(value) click to toggle source

Method to have only one variable in the class to save the temperature. We use Kelvin. For this reason, we convert celsius to kelvin

# File lib/temperatures.rb, line 21
def celsius= (value)
  @kelvin = value + KELVINOFFSET
end
fahrenheit=(value) click to toggle source

Method to have only one variable in the class to save the temperature. We use Kelvin. For this reason, we convert fahrenheit to kelvin

# File lib/temperatures.rb, line 25
def fahrenheit=value
  @kelvin = ((value - OFFSETFAHRENHEIT) / SLOPEFAHRENHEIT) + KELVINOFFSET
end
to_celsius() click to toggle source

Method to return the actual value of degrees celsisus

# File lib/temperatures.rb, line 29
def to_celsius
  kelvin - KELVINOFFSET
end
to_fahrenheit() click to toggle source

Method to return the actual value of degrees fahrenheit

# File lib/temperatures.rb, line 37
def to_fahrenheit
  ((kelvin- KELVINOFFSET) * SLOPEFAHRENHEIT) + OFFSETFAHRENHEIT
end
to_kelvin() click to toggle source

Method to return the actual value of degrees kelvin

# File lib/temperatures.rb, line 33
def to_kelvin
  kelvin
end