Skip to content

Commit

Permalink
Finished alexch#10. Is happy.
Browse files Browse the repository at this point in the history
  • Loading branch information
joshwcomeau committed Apr 7, 2014
1 parent 0018774 commit cddb52b
Showing 1 changed file with 53 additions and 0 deletions.
53 changes: 53 additions & 0 deletions 10_temperature_object/temperature.rb
Original file line number Diff line number Diff line change
@@ -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

0 comments on commit cddb52b

Please sign in to comment.