-
Notifications
You must be signed in to change notification settings - Fork 12
Expand file tree
/
Copy pathRuby02_Control_Structures
More file actions
153 lines (118 loc) · 3.33 KB
/
Ruby02_Control_Structures
File metadata and controls
153 lines (118 loc) · 3.33 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
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
## Ruby
## Control Structures
## Ruby Control Structures - Each
# use control structure each to iterate through its collections
# given a method called scoring with an array passed as an argument. Elements of the array are of the class User
# User class has a method update_score
# task is to iterate through each of the elements in the array using each and call the method update_score on every element
# Option 1
def scoring(array)
# iterate through each of the element in array using *each* and call update_score on it
array.each do |user|
user.update_score
end
end
# Option 2
def scoring(array)
# iterate through each of the element in array using *each* and call update_score on it
array.each {
|user|
user.update_score
}
end
## Ruby Control Structures - Unless
# given a method called scoring with an array passed as an argument. Elements of the array are of the class User
# User has two public methods, is_admin? and update_score.
# task is to use the control structure unless and update all elements of the array who are not admins
# unless is the logical equivalent of if not
# Option 1
def scoring(array)
# update_score of every user in the array unless the user is admin
array.each do |user|
unless user.is_admin?
user.update_score
end
end
end
# Option 2
def scoring(array)
# update_score of every user in the array unless the user is admin
array.each do |user|
user.update_score unless user.is_admin?
end
end
# Option 3
def scoring(array)
# update_score of every user in the array unless the user is admin
array.each {
|user|
user.update_score unless user.is_admin?
}
end
## Ruby Control Structures - Infinite Loop
# An infinite loop in Ruby is of the form
loop do
end
# Use an infinite loop and call the method coder.practice within it and break if coder.oh_one? is true.
# Option 1
loop do
coder.practice
if coder.oh_one?
break
end
end
# Option 2
loop do
coder.practice
break if coder.oh_one?
end
## Ruby Control Structures - Until
# Call the method coder.practice until coder.oh_one? becomes true.
# Use the until control structure.
# until is the logical equivalent of while not.
# Option 1
while not coder.oh_one?
coder.practice
end
# Option 2
until coder.oh_one?
coder.practice
end
# Option 3
coder.practice until coder.oh_one?
## Ruby Control Structures - Case (Bonus Question)
# HackerRank is written in RoR and we have various classes defined in it.
# Option 1
def identify_class(obj)
# write your case control structure here
case obj
when Hacker, Submission, TestCase, Contest
puts "It's a #{obj.class}!"
else
puts "It's an unknown model"
end
end
# Option 2 unrefactored version
def identify_class(obj)
case obj
when Hacker
puts "It's a Hacker!"
when Submission
puts "It's a Submission!"
when TestCase
puts "It's a TestCase!"
when Contest
puts "It's a Contest!"
else
puts "It's an unknown model"
end
end
# # Option 3 (not working)
# def identify_class(obj)
# if obj == Hacker || obj == Submission || obj == TestCase || obj == Contest
# puts "It's a #{obj.class}!"
# else
# puts "It's an unknown model"
# end
# end
## end ##