Skip to content

Commit cddb52b

Browse files
committed
Finished alexch#10. Is happy.
1 parent 0018774 commit cddb52b

File tree

1 file changed

+53
-0
lines changed

1 file changed

+53
-0
lines changed

10_temperature_object/temperature.rb

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
class Temperature
2+
def initialize(temphash)
3+
@temp = temphash[:f] || temphash[:c]
4+
@unit = temphash.keys[0]
5+
end
6+
7+
def to_fahrenheit
8+
return @temp if @unit == :f
9+
((@temp * 9/5) + 32).round(1)
10+
end
11+
12+
def to_celsius
13+
return @temp if @unit == :c
14+
((@temp - 32) * 5/9).ceil
15+
end
16+
17+
def self.in_celsius(temp)
18+
Temperature.new({c: temp})
19+
end
20+
21+
def self.in_fahrenheit(temp)
22+
Temperature.new({f: temp})
23+
end
24+
25+
26+
end
27+
28+
class Celsius < Temperature
29+
def initialize(temp)
30+
@temp = temp
31+
@unit = :c
32+
end
33+
end
34+
35+
class Fahrenheit < Temperature
36+
def initialize(temp)
37+
@temp = temp
38+
@unit = :f
39+
end
40+
end
41+
42+
=begin
43+
44+
def ftoc(temp)
45+
( ( temp - 32 ) * 5/9 ).ceil
46+
end
47+
48+
def ctof(temp)
49+
((temp * 9/5)+ 32).round(1)
50+
end
51+
52+
53+
=end

0 commit comments

Comments
 (0)