Created
October 27, 2012 09:43
-
-
Save kennyt/3963791 to your computer and use it in GitHub Desktop.
08_temperature
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class Temperature | |
def initialize (input) | |
@temperature = input | |
end | |
def ftoc (number) | |
number * 9/5.to_f + 32 | |
end | |
def ctof (number) | |
(number - 32) * 5/9.to_f | |
end | |
def self.from_fahrenheit (fahrenheit) | |
Temperature.new(:f => fahrenheit) | |
end | |
def self.from_celsius (celsius) | |
Temperature.new(:c => celsius) | |
end | |
def in_fahrenheit | |
@temperature.keys == [:f] ? @temperature[:f] : ftoc(@temperature[:c]) | |
end | |
def in_celsius | |
@temperature.keys == [:c] ? @temperature[:c] : ctof(@temperature[:f]) | |
end | |
end | |
class Celsius < Temperature | |
def initialize (temperature) | |
@temperature = {:c => temperature} | |
end | |
end | |
class Fahrenheit < Temperature | |
def initialize (temperature) | |
@temperature = {:f => temperature} | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment