File tree Expand file tree Collapse file tree 1 file changed +53
-0
lines changed Expand file tree Collapse file tree 1 file changed +53
-0
lines changed Original file line number Diff line number Diff line change
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
You can’t perform that action at this time.
0 commit comments