/
LRTHW37.rb
200 lines (181 loc) · 4.74 KB
/
LRTHW37.rb
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
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
# module LRTHW37 # module methods must have self? outside module must call module name?
# maybe modules can only have function definitions? no execution of Main?
# can't call gets.chomp in a script with a module?
# From Rubyist.net: a module can have no instances and no subclasses
# hence, it seems that it is impossible for me to ask user input and have a module here.
# save module learning for later
# let's think car models and brands
# write a script that asks for a set of car models, then pair them with the brands below.
# if model is unknown, rescue/end safely without raising an exception.
# There are four ways to interrupt the progress of a loop from inside.
# First, break means, as in C, to escape from the loop entirely.
# Second, next skips to the beginning of the next iteration of the loop.
# Third, ruby has redo, which restarts the current iteration.
# The fourth way to get out of a loop from the inside is return.
def what_is_this_car(car)
manufacturer = case car
when "Saga" then "Proton"
when "Focus" then "Ford"
when "Navigator" then "Lincoln"
when "Camry" then "Toyota"
when "Civic" then "Honda"
when "Patriot" then "Jeep"
when "Jetta" then "VW"
when "Cayenne" then "Porsche"
when "Outback" then "Subaru"
when "520i" then "BMW"
when "Tundra" then "Nissan"
else "Unknown"
end
rescue
manufacturer = "Unknown"
return manufacturer
ensure
puts "Thanks for playing."
end
car_name = gets.chomp
puts what_is_this_car(car_name)
# RESCUE
# an example exists on the desktop if you're still unsure
# ENSURE
# begin
# file = open("/tmp/some_file", "w")
# # ... write to the file ...
# rescue
# # ... handle the exceptions ...
# ensure
# file.close # ... and this always happens.
# end
# if rescue and ensure both exist, rescue must come first before ensure
# END executes before the script exits regardless of earlier bits.
# REDO
# #!/usr/bin/ruby
#
# for i in 0..5
# if i < 2 then
# puts "Value of local variable is #{i}"
# redo
# end
# end
# This will produce following result and will go in an infinite loop:
#
# Value of local variable is 0
# Value of local variable is 0
# ............................
# NEXT
#!/usr/bin/ruby
#
# for i in 0..5
# if i < 2 then
# next
# end
# puts "Value of local variable is #{i}"
# end
# This will produce following result:
#
# Value of local variable is 2
# Value of local variable is 3
# Value of local variable is 4
# Value of local variable is 5
# BREAK
#!/usr/bin/ruby
#
# for i in 0..5
# if i > 2 then
# break
# end
# puts "Value of local variable is #{i}"
# end
# This will produce following result:
#
# Value of local variable is 0
# Value of local variable is 1
# Value of local variable is 2
# RETRY
#!/usr/bin/ruby
#
# for i in 1..5
# retry if i > 2
# puts "Value of local variable is #{i}"
# end
# This will produce following result and will go in an infinite loop:
#
# Value of local variable is 1
# Value of local variable is 2
# Value of local variable is 1
# Value of local variable is 2
# Value of local variable is 1
# Value of local variable is 2
# ............................
# car = "Patriot"
# UNTIL
# Ruby until Statement:
# Syntax:
#
# until conditional [do]
# code
# end
# Executes code while conditional is false. An until statement's conditional is separated from code by the reserved word do, a newline, or a semicolon.
#
# Example:
#
# #!/usr/bin/ruby
#
# $i = 0;
# $num = 5;
#
# until $i > $num do
# puts("Inside the loop i = #$i" );
# $i +=1;
# end
# This will produce following result:
#
# Inside the loop i = 0
# Inside the loop i = 1
# Inside the loop i = 2
# Inside the loop i = 3
# Inside the loop i = 4
# Inside the loop i = 5
# Ruby until modifier:
# Syntax:
#
# code until conditional
#
# OR
#
# begin
# code
# end until conditional
# Executes code while conditional is false.
#
# If an until modifier follows a begin statement with no rescue or ensure clauses, code is executed once before conditional is evaluated.
#
# Example:
#
# #!/usr/bin/ruby
#
# $i = 0;
# $num = 5;
# begin
# puts("Inside the loop i = #$i" );
# $i +=1;
# end until $i > $num
# This will produce following result:
#
# Inside the loop i = 0
# Inside the loop i = 1
# Inside the loop i = 2
# Inside the loop i = 3
# Inside the loop i = 4
# Inside the loop i = 5
# super. couldn't find a good explanation for super.
# undef method_name
# can't be in the method def body.
# unless x == y, is same as if x != y
# just an inverted if
# while
# you know what it's about already. have confidence you can sort it out.
# yield
# http://fairleads.blogspot.com/2007/06/ruby-yield.html
# until is to while as if is to unless
# http://www.techotopia.com/index.php/Ruby_While_and_Until_Loops#unless_and_until