Skip to content

Instantly share code, notes, and snippets.

@thomasbcolley
Created July 14, 2015 02:14
Show Gist options
  • Save thomasbcolley/5f114492fedf3b611407 to your computer and use it in GitHub Desktop.
Save thomasbcolley/5f114492fedf3b611407 to your computer and use it in GitHub Desktop.
# Student: Thomas-0725;
# Status: Complete;
class Temperature
#Temperature is stored in @temperature_celsius and then convereted to other systems as needed.
private
def ftoc(fahrenheit)
celsius = ((fahrenheit - 32.0) * 5.0) / 9.0
end
def ctof(celsius)
fahrenheit = (celsius * 9.0) / 5.0 + 32.0
end
public
#Takes options c: temp or f: temp. :c option is given precedence.
def initialize(options)
@temperature_celsius = Float(options[:c]) rescue ftoc(Float(options[:f])) rescue nil
=begin #OLD
case true
when options[:c].is_a?(Numeric)
@temperature_celsius = options[:c].to_f
when options[:f].is_a?(Numeric)
@temperature_celsius = ftoc(options[:f])
else
@temperature_celsius = nil
end
=end
end
#Returns temperature of object, in Fahrenheit.
def in_fahrenheit
ctof(@temperature_celsius)
end
#Returns temperature of object, in Celsius.
def in_celsius
@temperature_celsius
end
#Returns a new Temperature object created from a Celsius temperature.
def Temperature.from_celsius(celsius)
Temperature.new(:c => celsius)
end
#Returns a new Temperature object created from a Fahrenheit temperature.
def Temperature.from_fahrenheit(fahrenheit)
Temperature.new(:f => fahrenheit)
end
end
class Celsius < Temperature
def initialize(degrees_celsius)
@temperature_celsius = degrees_celsius
end
end
class Fahrenheit < Temperature
def initialize(degrees_fahrenheit)
@temperature_celsius = ftoc(degrees_fahrenheit)
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment