From cddb52b015845d5bf139a0dcb032227d80be7e10 Mon Sep 17 00:00:00 2001 From: Josh Comeau Date: Mon, 7 Apr 2014 16:39:23 -0400 Subject: [PATCH] Finished #10. Is happy. --- 10_temperature_object/temperature.rb | 53 ++++++++++++++++++++++++++++ 1 file changed, 53 insertions(+) create mode 100644 10_temperature_object/temperature.rb diff --git a/10_temperature_object/temperature.rb b/10_temperature_object/temperature.rb new file mode 100644 index 000000000..7f5587f4f --- /dev/null +++ b/10_temperature_object/temperature.rb @@ -0,0 +1,53 @@ +class Temperature + def initialize(temphash) + @temp = temphash[:f] || temphash[:c] + @unit = temphash.keys[0] + end + + def to_fahrenheit + return @temp if @unit == :f + ((@temp * 9/5) + 32).round(1) + end + + def to_celsius + return @temp if @unit == :c + ((@temp - 32) * 5/9).ceil + end + + def self.in_celsius(temp) + Temperature.new({c: temp}) + end + + def self.in_fahrenheit(temp) + Temperature.new({f: temp}) + end + + +end + +class Celsius < Temperature + def initialize(temp) + @temp = temp + @unit = :c + end +end + +class Fahrenheit < Temperature + def initialize(temp) + @temp = temp + @unit = :f + end +end + +=begin + +def ftoc(temp) + ( ( temp - 32 ) * 5/9 ).ceil +end + +def ctof(temp) + ((temp * 9/5)+ 32).round(1) +end + + +=end \ No newline at end of file