Skip to content

Instantly share code, notes, and snippets.

@rhysforyou
Created September 7, 2010 04:24
Show Gist options
  • Save rhysforyou/567864 to your computer and use it in GitHub Desktop.
Save rhysforyou/567864 to your computer and use it in GitHub Desktop.
class Temperature < Numeric
def initialize(temp)
@temp = temp
super
end
def to_farenheit
9 / 5 * (@temp + 32)
end
def to_farenheit!
@temp = 9 / 5 * (@temp + 32)
end
def to_celcius
5 / 9 * (@temp - 32)
end
def to_celcius!
@temp = 5 / 9 * (@temp - 32)
end
end
=begin
doctest: From celcius to farenheit
>> Temperature.new(40).to_farenheits
=> 72
=end
=begin
doctest: From farenheit to celcius
>> Temperature.new(72).to_celcius
=> 40
=end
=begin
doctest: From celcius to farenheit permanent
>> temp = Temperature.new(40).to_farenheit!
>> temp
=> 72
=end
=begin
doctest: From farenheit to celcius permanent
>> temp = Temperature.new(72).to_celcius!
>> temp
=> 40
=end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment