Skip to content

Instantly share code, notes, and snippets.

@kennyt
Created October 27, 2012 09:43
Show Gist options
  • Save kennyt/3963791 to your computer and use it in GitHub Desktop.
Save kennyt/3963791 to your computer and use it in GitHub Desktop.
08_temperature
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