-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathoven.rb
More file actions
99 lines (82 loc) · 1.63 KB
/
oven.rb
File metadata and controls
99 lines (82 loc) · 1.63 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
class OvenOffError < StandardError
end
class OvenEmptyError < StandardError
end
class SmallOven
attr_accessor :contents
def turn_on
puts "Turning oven on."
@state = "on"
end
def turn_off
puts "Turning oven off"
@state = "off"
end
def bake
unless @state == "on"
raise OvenOffError, "You need to turn the oven on first"
end
if @contents == nil
raise OvenEmptyError, "There`s nothing in the oven!"
end
"golden-brown #{contents}"
end
end
dinner = ['turkey', nil, 'pie']
oven = SmallOven.new
dinner.each do |item|
begin
oven.turn_on
oven.contents = item
puts "Serving #{oven.bake}"
rescue OvenEmptyError => error
puts "Error: #{error.message}"
ensure
oven.turn_off
end
end
=begin
class TestScoreError < StandardError
end
score = 52
begin
if score > 60
puts "passing grade"
else
raise TestScoreError, "failing grade"
end
rescue TestScoreError => error
puts "Received #{error.message}. Taking make-up exam..."
score = 63
retry
end
# practice 1
def drive(destination)
if destination == "Hawaii"
raise "You can't drive to Hawaii!"
end
end
begin
drive("Hawaii")
rescue => error
puts error.message
end
# practice 2
class PorridgeError < StandardError
end
class BeddingError < StandardError
end
def eat
raise PorridgeError, "too hot"
end
def sleep
raise BeddingError, "too soft"
end
begin
sleep
rescue BeddingError => error
puts "This bed is #{error.message}!"
rescue PorridgeError => error
puts "This porridge is #{error.message}!"
end
=end