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
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
|
a = b.foo
if a.empty?
a = b.bar
else
a.reverse!
end
if (a = b.foo).empty?
a = b.bar
else
a.reverse!
end
-----------------------------------------------
case savings_account
when "not much"
buy_bike
when "some dough"
buy_yugo
when "a good wad in the wallet"
buy_toyota
when "just won the lottery"
buy_dream_car
end
-----------------------------------------------
breakfast = nil
if breakfeast == nil
breakfast = "bacon and egss"
end
if breakfast.nil?
breakfast = "bacon and eggs"
end
if !breakfast
breakfast = "bacon and eggs"
end
unless breakfeast
breakfast = "bacon and eggs"
end
breakfast = "bacon and eggs" unless breakfast
breakfast ||= "bacon and eggs"
-----------------------------------------------
val = 12
if val == 1 || val == 12 || val == 42
puts "woo hoo!"
end
if [1,12,42].include?(val)
puts "woo hoo!"
end
-----------------------------------------------
fruits = ['apple','banana']
fruits << 'apple' unless fruits.include?('apple')
fruits |= ['apple']
-----------------------------------------------
user_id = nil
if comments
if comments.first
if comments.first.user
user_id = comments.first.user.id
end
end
end
user_id = comments && comments.first && comments.first.user && comments.first.user.id
-----------------------------------------------
def say_goodnight(name)
result = "Good night, " + name.capitalize
return result
end
def say_goodnight(name)
"Good night, #{name,capitalize}"
end
-----------------------------------------------
def old_enough?(age)
if age > 15
true
else
false
end
end
def old_enough?(age)
age > 15
end
-----------------------------------------------
class Person
def initialize(name)
@name = name
end
def name
@name
end
def name=(name)
@name = name
end
end
class Person
attr_accessor :name
def initialize(name)
@name = name
end
end
-----------------------------------------------
a = [ 'ant', 'bee', 'cat', 'dog', 'elk' ]
a = %w{ ant bee cat dog elk }
-----------------------------------------------
for i in 0..9
puts i, " "
end
10.times {|i| puts i}
-----------------------------------------------
people = array of people...
i = 0
while i < people.size
puts people[i].name
i += 1
end
for i in 0..people.size
puts people[i].name
end
people.each { |person| puts person.name }
-----------------------------------------------
def capital_letters
('A'..'Z').to_a.to_a
end
@@capital_letters=('A'..'Z').to_a
def capital_letters
@@capital_letters
end
-----------------------------------------------
def stringify_array(an_array)
a_string = ""
for element in an_array
if a_string.size > 0
a_string += ", "
end
a_string += element.to_s
end
return a_string
end
some_array = %w(hello there buddy)
puts stringify_array(some_array)
puts some_array.join(", ")
|