summaryrefslogtreecommitdiff
path: root/doc/syntax/control_expressions.rdoc
blob: 18dd40b676d63bc75e008314e6eaa9388343e888 (plain)
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
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
= Control Expressions

Ruby has a variety of ways to control execution.  All the expressions described
here return a value.

For the tests in these control expressions, +nil+ and +false+ are false-values
and +true+ and any other object are true-values.  In this document "true" will
mean "true-value" and "false" will mean "false-value".

== +if+ Expression

The simplest +if+ expression has two parts, a "test" expression and a "then"
expression.  If the "test" expression evaluates to a true then the "then"
expression is evaluated.

Here is a simple if statement:

  if true then
    puts "the test resulted in a true-value"
  end

This will print "the test resulted in a true-value".

The +then+ is optional:

  if true
    puts "the test resulted in a true-value"
  end

This document will omit the optional +then+ for all expressions as that is the
most common usage of +if+.

You may also add an +else+ expression.  If the test does not evaluate to true
the +else+ expression will be executed:

  if false
    puts "the test resulted in a true-value"
  else
    puts "the test resulted in a false-value"
  end

This will print "the test resulted in a false-value".

You may add an arbitrary number of extra tests to an if expression using
+elsif+.  An +elsif+ executes when all tests above the +elsif+ are false.

  a = 1

  if a == 0
    puts "a is zero"
  elsif a == 1
    puts "a is one"
  else
    puts "a is some other value"
  end

This will print "a is one" as <code>1</code> is not equal to <code>0</code>.
Since +else+ is only executed when there are no matching conditions.

Once a condition matches, either the +if+ condition or any +elsif+ condition,
the +if+ expression is complete and no further tests will be performed.

Like an +if+, an +elsif+ condition may be followed by a +then+.

In this example only "a is one" is printed:

  a = 1

  if a == 0
    puts "a is zero"
  elsif a == 1
    puts "a is one"
  elsif a >= 1
    puts "a is greater than or equal to one"
  else
    puts "a is some other value"
  end

The tests for +if+ and +elsif+ may have side-effects.  The most common use of
side-effect is to cache a value into a local variable:

  if a = object.some_value
    # do something to a
  end

The result value of an +if+ expression is the last value executed in the
expression.

== +unless+ Expression

The +unless+ expression is the opposite of the +if+ expression.  If the value
is false the "then" expression is executed:

  unless true
    puts "the value is a false-value"
  end

This prints nothing as true is not a false-value.

You may use an optional +then+ with +unless+ just like +if+.

Note that the above +unless+ expression is the same as:

  if not true
    puts "the value is a false-value"
  end

Like an +if+ expression you may use an +else+ condition with +unless+:

  unless true
    puts "the value is false"
  else
    puts "the value is true"
  end

This prints "the value is true" from the +else+ condition.

You may not use +elsif+ with an +unless+ expression.

The result value of an +unless+ expression is the last value executed in the
expression.

== Modifier +if+ and +unless+

+if+ and +unless+ can also be used to modify an expression.  When used as a
modifier the left-hand side is the "then" expression and the right-hand side
is the "test" expression:

  a = 0

  a += 1 if a.zero?

  p a

This will print 1.

  a = 0

  a += 1 unless a.zero?

  p a

This will print 0.

While the modifier and standard versions have both a "test" expression and a
"then" expression, they are not exact transformations of each other due to
parse order.  Here is an example that shows the difference:

  p a if a = 0.zero?

This raises the NameError "undefined local variable or method `a'".

When ruby parses this expression it first encounters +a+ as a method call in
the "then" expression, then later it sees the assignment to +a+ in the "test"
expression and marks +a+ as a local variable.

When running this line it first executes the "test" expression, <code>a =
0.zero?</code>.

Since the test is true it executes the "then" expression, <code>p a</code>.
Since the +a+ in the body was recorded as a method which does not exist the
NameError is raised.

The same is true for +unless+.

== +case+ Expression

The +case+ expression can be used in two ways.

The most common way is to compare an object against multiple patterns.  The
patterns are matched using the +===+ method which is aliased to +==+ on
Object.  Other classes must override it to give meaningful behavior.  See
Module#=== and Regexp#=== for examples.

Here is an example of using +case+ to compare a String against a pattern:

  case "12345"
  when /^1/
    puts "the string starts with one"
  else
    puts "I don't know what the string starts with"
  end

Here the string <code>"12345"</code> is compared with <code>/^1/</code> by
calling <code>/^1/ === "12345"</code> which returns +true+.  Like the +if+
expression the first +when+ that matches is executed and all other matches are
ignored.

If no matches are found the +else+ is executed.

The +else+ and +then+ are optional, this +case+ expression gives the same
result as the one above:

  case "12345"
  when /^1/
    puts "the string starts with one"
  end

You may place multiple conditions on the same +when+:

  case "2"
  when /^1/, "2"
    puts "the string starts with one or is '2'"
  end

Ruby will try each condition in turn, so first <code>/^1/ === "2"</code>
returns +false+, then <code>"2" === "2"</code> returns +true+, so "the string
starts with one or is '2'" is printed.

You may use +then+ after the +when+ condition.  This is most frequently used
to place the body of the +when+ on a single line.

  case a
  when 1, 2 then puts "a is one or two
  when 3    then puts "a is three"
  else           puts "I don't know what a is"
  end

The other way to use a +case+ expression is like an if-elsif expression:

  a = 2

  case
  when a == 1, a == 2
    puts "a is one or two"
  when a == 3
    puts "a is three"
  else
    puts "I don't know what a is"
  end

Again, the +then+ and +else+ are optional.

The result value of a +case+ expression is the last value executed in the
expression.

== +while+ Loop

The +while+ loop executes while a condition is true:

  a = 0

  while a < 10 do
    p a
    a += 1
  end

  p a

Prints the numbers 0 through 10.  The condition <code>a < 10</code> is checked
before the loop is entered, then the body executes, then the condition is
checked again.  When the condition results in false the loop is terminated.

The +do+ keyword is optional.  The following loop is equivalent to the loop
above:

  while a < 10
    p a
    a += 1
  end

The result of a +while+ loop is +nil+ unless +break+ is used to supply a
value.

== +until+ Loop

The +until+ loop executes while a condition is false:

  a = 0

  until a > 10 do
    p a
    a += 1
  end

  p a

This prints the numbers 0 through 11.  Like a while loop the condition <code>a
> 10</code> is checked when entering the loop and each time the loop body
executes.  If the condition is false the loop will continue to execute.

Like a +while+ loop the +do+ is optional.

Like a +while+ loop the result of an +until+ loop is nil unless +break+ is
used.

== +for+ Loop

The +for+ loop consists of +for+ followed by a variable to contain the
iteration argument followed by +in+ and the value to iterate over using #each.
The +do+ is optional:

  for value in [1, 2, 3] do
    puts value
  end

Prints 1, 2 and 3.

Like +while+ and +until+, the +do+ is optional.

The +for+ loop is similar to using #each, but does not create a new variable
scope.

The result value of a +for+ loop is the value iterated over unless +break+ is
used.

The +for+ loop is rarely used in modern ruby programs.

== Modifier +while+ and +until+

Like +if+ and +unless+, +while+ and +until+ can be used as modifiers:

  a = 0

  a += 1 while a < 10

  p a # prints 10

+until+ used as a modifier:

  a = 0

  a += 1 until a > 10

  p a # prints 11

You can use +begin+ and +end+ to create a +while+ loop that runs the body once
before the condition:

  a = 0

  begin
    a += 1
  end while a < 10

  p a # prints 10

If you don't use +rescue+ or +ensure+ Ruby optimizes away any exception
handling overhead.

== +break+ Statement

Use +break+ to leave a block early.  This will stop iterating over the items in +values+ if one of them is even:

  values.each do |value|
    break if value.even?

    # ...
  end

You can also terminate from a +while+ loop using +break+:

  a = 0

  while true do
    p a
    a += 1

    break if a < 10
  end

  p a

This prints the numbers 0 and 1.

+break+ accepts a value that supplies the result of the expression it is
"breaking" out of:

  result = [1, 2, 3].each do |value|
    break value * 2 if value.even?
  end

  p result # prints 4

== +next+ Statement

Use +next+ to skip the rest of the current iteration:

  result = [1, 2, 3].map do |value|
    next if value.even?

    value * 2
  end

  p result # prints [2, nil, 6]

+next+ accepts an argument that can be used the result of the current block
iteration:

  result = [1, 2, 3].map do |value|
    next value if value.even?

    value * 2
  end

  p result # prints [2, 2, 6]

== +redo+ Statement

Use +redo+ to redo the current iteration:

  result = []

  while result.length < 10 do
    result << result.length

    redo if result.last.even?

    result << result.length + 1
  end

  p result

This prints [0, 1, 3, 3, 5, 5, 7, 7, 9, 9, 11]

In Ruby 1.8 you could also use +retry+ where you used +redo+.  This is no
longer true, now you will receive a SyntaxError when you use +retry+ outside
of a +rescue+ block.  See {Exceptions}[rdoc-ref:syntax/exceptions.rdoc]
for proper usage of +retry+.