-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathfile.control_expressions.html
498 lines (320 loc) · 37.5 KB
/
file.control_expressions.html
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
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
<!DOCTYPE html>
<html>
<head>
<meta charset='UTF-8'>
<meta name='viewport' content='width=device-width, initial-scale=1.0, user-scalable=no'>
<meta name='apple-touch-fullscreen' content='yes'>
<meta name='apple-mobile-web-app-capable' content='yes'>
<meta name='apple-mobile-web-app-status-bar-style' content='rgba(228,228,228,1.0)'>
<title>File: Control Expressions — Ruby-2.6.10</title>
<link rel='stylesheet' type='text/css' href='../css/y_fonts.css' />
<link rel='stylesheet' type='text/css' href='../css/highlight.github.css' />
<link rel='stylesheet' type='text/css' href='../css/y_style.css' />
<link rel='stylesheet' type='text/css' href='../css/y_list.css' />
<link rel='stylesheet' type='text/css' href='../css/y_color.css' />
<script type='text/javascript'>
var pathId = "control_expressions",
relpath = '';
var t2Info = {
CSEP: '.',
ISEP: '#',
NSEP: '::'
};
</script>
<script type='text/javascript' charset='utf-8' src='../js/highlight.pack.js'></script>
<script type='text/javascript' charset='utf-8' src='../js/y_app.js'></script>
</head>
<body>
<svg id='y_wait' class viewBox='0 0 90 90'></svg>
<div id='settings' class='hidden'></div>
<div id='y_list' class='d h'>
<header id='list_header'></header>
<nav id= 'list_nav' class='y_nav l_nav'>
<ul id='list_items'></ul>
</nav>
</div>
<div id='y_toc' class='f h'>
<header id='toc_header'></header>
<nav id= 'toc_nav' class='y_nav t_nav'>
<ol id='toc_items'></ol>
</nav>
</div>
<div id='y_main' tabindex='-1'>
<header id='y_header'>
<div id='y_menu'>
<a id='home_no_xhr' href='/'>Home</a> »
<a href='.'>Ruby-2.6.10</a> »
<a href='_index.html'>Index</a> »
<span class='title'><a id='t2_doc_top' href='#'>File: Control Expressions ▲</a></span>
</div>
<a id='list_href' href="file_list.html"></a>
<div id='y_measure_em' class='y_measure'></div>
<div id='y_measure_vh' class='y_measure'></div>
<span id='y_measure_50pre' class='y_measure'><code>123456789_123456789_123456789_123456789_123456789_</code></span>
</header>
<div id='content' class='file'>
<h1 id="label-Control+Expressions">Control Expressions</h1>
<p>Ruby has a variety of ways to control execution. All the expressions described here return a value.</p>
<p>For the tests in these control expressions, <code>nil</code> and <code>false</code> are false-values and <code>true</code> and any other object are true-values. In this document “true” will mean “true-value” and “false” will mean “false-value”.</p>
<h2 id="label-if+Expression"><code>if</code> Expression</h2>
<p>The simplest <code>if</code> 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.</p>
<p>Here is a simple if statement:</p>
<pre class="code ruby"><code class="ruby"><span class='kw'>if</span> <span class='kw'>true</span> <span class='kw'>then</span>
<span class='id identifier rubyid_puts'>puts</span> <span class='tstring'><span class='tstring_beg'>"</span><span class='tstring_content'>the test resulted in a true-value</span><span class='tstring_end'>"</span></span>
<span class='kw'>end</span></code></pre>
<p>This will print “the test resulted in a true-value”.</p>
<p>The <code>then</code> is optional:</p>
<pre class="code ruby"><code class="ruby"><span class='kw'>if</span> <span class='kw'>true</span>
<span class='id identifier rubyid_puts'>puts</span> <span class='tstring'><span class='tstring_beg'>"</span><span class='tstring_content'>the test resulted in a true-value</span><span class='tstring_end'>"</span></span>
<span class='kw'>end</span></code></pre>
<p>This document will omit the optional <code>then</code> for all expressions as that is the most common usage of <code>if</code>.</p>
<p>You may also add an <code>else</code> expression. If the test does not evaluate to true the <code>else</code> expression will be executed:</p>
<pre class="code ruby"><code class="ruby"><span class='kw'>if</span> <span class='kw'>false</span>
<span class='id identifier rubyid_puts'>puts</span> <span class='tstring'><span class='tstring_beg'>"</span><span class='tstring_content'>the test resulted in a true-value</span><span class='tstring_end'>"</span></span>
<span class='kw'>else</span>
<span class='id identifier rubyid_puts'>puts</span> <span class='tstring'><span class='tstring_beg'>"</span><span class='tstring_content'>the test resulted in a false-value</span><span class='tstring_end'>"</span></span>
<span class='kw'>end</span></code></pre>
<p>This will print “the test resulted in a false-value”.</p>
<p>You may add an arbitrary number of extra tests to an if expression using <code>elsif</code>. An <code>elsif</code> executes when all tests above the <code>elsif</code> are false.</p>
<pre class="code ruby"><code class="ruby"><span class='id identifier rubyid_a'>a</span> <span class='op'>=</span> <span class='int'>1</span>
<span class='kw'>if</span> <span class='id identifier rubyid_a'>a</span> <span class='op'>==</span> <span class='int'>0</span>
<span class='id identifier rubyid_puts'>puts</span> <span class='tstring'><span class='tstring_beg'>"</span><span class='tstring_content'>a is zero</span><span class='tstring_end'>"</span></span>
<span class='kw'>elsif</span> <span class='id identifier rubyid_a'>a</span> <span class='op'>==</span> <span class='int'>1</span>
<span class='id identifier rubyid_puts'>puts</span> <span class='tstring'><span class='tstring_beg'>"</span><span class='tstring_content'>a is one</span><span class='tstring_end'>"</span></span>
<span class='kw'>else</span>
<span class='id identifier rubyid_puts'>puts</span> <span class='tstring'><span class='tstring_beg'>"</span><span class='tstring_content'>a is some other value</span><span class='tstring_end'>"</span></span>
<span class='kw'>end</span></code></pre>
<p>This will print “a is one” as <code>1</code> is not equal to <code>0</code>. Since <code>else</code> is only executed when there are no matching conditions.</p>
<p>Once a condition matches, either the <code>if</code> condition or any <code>elsif</code> condition, the <code>if</code> expression is complete and no further tests will be performed.</p>
<p>Like an <code>if</code>, an <code>elsif</code> condition may be followed by a <code>then</code>.</p>
<p>In this example only “a is one” is printed:</p>
<pre class="code ruby"><code class="ruby"><span class='id identifier rubyid_a'>a</span> <span class='op'>=</span> <span class='int'>1</span>
<span class='kw'>if</span> <span class='id identifier rubyid_a'>a</span> <span class='op'>==</span> <span class='int'>0</span>
<span class='id identifier rubyid_puts'>puts</span> <span class='tstring'><span class='tstring_beg'>"</span><span class='tstring_content'>a is zero</span><span class='tstring_end'>"</span></span>
<span class='kw'>elsif</span> <span class='id identifier rubyid_a'>a</span> <span class='op'>==</span> <span class='int'>1</span>
<span class='id identifier rubyid_puts'>puts</span> <span class='tstring'><span class='tstring_beg'>"</span><span class='tstring_content'>a is one</span><span class='tstring_end'>"</span></span>
<span class='kw'>elsif</span> <span class='id identifier rubyid_a'>a</span> <span class='op'>>=</span> <span class='int'>1</span>
<span class='id identifier rubyid_puts'>puts</span> <span class='tstring'><span class='tstring_beg'>"</span><span class='tstring_content'>a is greater than or equal to one</span><span class='tstring_end'>"</span></span>
<span class='kw'>else</span>
<span class='id identifier rubyid_puts'>puts</span> <span class='tstring'><span class='tstring_beg'>"</span><span class='tstring_content'>a is some other value</span><span class='tstring_end'>"</span></span>
<span class='kw'>end</span></code></pre>
<p>The tests for <code>if</code> and <code>elsif</code> may have side-effects. The most common use of side-effect is to cache a value into a local variable:</p>
<pre class="code ruby"><code class="ruby"><span class='kw'>if</span> <span class='id identifier rubyid_a'>a</span> <span class='op'>=</span> <span class='id identifier rubyid_object'>object</span>.<span class='id identifier rubyid_some_value'>some_value</span>
<span class='comment'># do something to a
</span><span class='kw'>end</span></code></pre>
<p>The result value of an <code>if</code> expression is the last value executed in the expression.</p>
<h2 id="label-Ternary+if">Ternary if</h2>
<p>You may also write a if-then-else expression using <code>?</code> and <code>:</code>. This ternary if:</p>
<pre class="code ruby"><code class="ruby"><span class='id identifier rubyid_input_type'>input_type</span> <span class='op'>=</span> <span class='id identifier rubyid_gets'>gets</span> <span class='op'>=~</span> <span class='tstring'><span class='regexp_beg'>/</span><span class='tstring_content'>hello</span><span class='regexp_end'>/i</span></span> <span class='op'>?</span> <span class='tstring'><span class='tstring_beg'>"</span><span class='tstring_content'>greeting</span><span class='tstring_end'>"</span></span> <span class='op'>:</span> <span class='tstring'><span class='tstring_beg'>"</span><span class='tstring_content'>other</span><span class='tstring_end'>"</span></span></code></pre>
<p>Is the same as this <code>if</code> expression:</p>
<pre class="code ruby"><code class="ruby"><span class='id identifier rubyid_input_type'>input_type</span> <span class='op'>=</span>
<span class='kw'>if</span> <span class='id identifier rubyid_gets'>gets</span> <span class='op'>=~</span> <span class='tstring'><span class='regexp_beg'>/</span><span class='tstring_content'>hello</span><span class='regexp_end'>/i</span></span>
<span class='tstring'><span class='tstring_beg'>"</span><span class='tstring_content'>greeting</span><span class='tstring_end'>"</span></span>
<span class='kw'>else</span>
<span class='tstring'><span class='tstring_beg'>"</span><span class='tstring_content'>other</span><span class='tstring_end'>"</span></span>
<span class='kw'>end</span></code></pre>
<p>While the ternary if is much shorter to write than the more verbose form, for readability it is recommended that the ternary if is only used for simple conditionals. Also, avoid using multiple ternary conditions in the same expression as this can be confusing.</p>
<h2 id="label-unless+Expression"><code>unless</code> Expression</h2>
<p>The <code>unless</code> expression is the opposite of the <code>if</code> expression. If the value is false, the “then” expression is executed:</p>
<pre class="code ruby"><code class="ruby"><span class='kw'>unless</span> <span class='kw'>true</span>
<span class='id identifier rubyid_puts'>puts</span> <span class='tstring'><span class='tstring_beg'>"</span><span class='tstring_content'>the value is a false-value</span><span class='tstring_end'>"</span></span>
<span class='kw'>end</span></code></pre>
<p>This prints nothing as true is not a false-value.</p>
<p>You may use an optional <code>then</code> with <code>unless</code> just like <code>if</code>.</p>
<p>Note that the above <code>unless</code> expression is the same as:</p>
<pre class="code ruby"><code class="ruby"><span class='kw'>if</span> <span class='kw'>not</span> <span class='kw'>true</span>
<span class='id identifier rubyid_puts'>puts</span> <span class='tstring'><span class='tstring_beg'>"</span><span class='tstring_content'>the value is a false-value</span><span class='tstring_end'>"</span></span>
<span class='kw'>end</span></code></pre>
<p>Like an <code>if</code> expression you may use an <code>else</code> condition with <code>unless</code>:</p>
<pre class="code ruby"><code class="ruby"><span class='kw'>unless</span> <span class='kw'>true</span>
<span class='id identifier rubyid_puts'>puts</span> <span class='tstring'><span class='tstring_beg'>"</span><span class='tstring_content'>the value is false</span><span class='tstring_end'>"</span></span>
<span class='kw'>else</span>
<span class='id identifier rubyid_puts'>puts</span> <span class='tstring'><span class='tstring_beg'>"</span><span class='tstring_content'>the value is true</span><span class='tstring_end'>"</span></span>
<span class='kw'>end</span></code></pre>
<p>This prints “the value is true” from the <code>else</code> condition.</p>
<p>You may not use <code>elsif</code> with an <code>unless</code> expression.</p>
<p>The result value of an <code>unless</code> expression is the last value executed in the expression.</p>
<h2 id="label-Modifier+if+and+unless">Modifier <code>if</code> and <code>unless</code></h2>
<p><code>if</code> and <code>unless</code> 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:</p>
<pre class="code ruby"><code class="ruby"><span class='id identifier rubyid_a'>a</span> <span class='op'>=</span> <span class='int'>0</span>
<span class='id identifier rubyid_a'>a</span> <span class='op'>+=</span> <span class='int'>1</span> <span class='kw'>if</span> <span class='id identifier rubyid_a'>a</span>.<span class='id identifier rubyid_zero?'>zero?</span>
<span class='id identifier rubyid_p'>p</span> <span class='id identifier rubyid_a'>a</span></code></pre>
<p>This will print 1.</p>
<pre class="code ruby"><code class="ruby"><span class='id identifier rubyid_a'>a</span> <span class='op'>=</span> <span class='int'>0</span>
<span class='id identifier rubyid_a'>a</span> <span class='op'>+=</span> <span class='int'>1</span> <span class='kw'>unless</span> <span class='id identifier rubyid_a'>a</span>.<span class='id identifier rubyid_zero?'>zero?</span>
<span class='id identifier rubyid_p'>p</span> <span class='id identifier rubyid_a'>a</span></code></pre>
<p>This will print 0.</p>
<p>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>
<pre class="code ruby"><code class="ruby"><span class='id identifier rubyid_p'>p</span> <span class='id identifier rubyid_a'>a</span> <span class='kw'>if</span> <span class='id identifier rubyid_a'>a</span> <span class='op'>=</span> <span class='int'>0</span>.<span class='id identifier rubyid_zero?'>zero?</span></code></pre>
<p>This raises the NameError “undefined local variable or method ‘a’”.</p>
<p>When ruby parses this expression it first encounters <code>a</code> as a method call in the “then” expression, then later it sees the assignment to <code>a</code> in the “test” expression and marks <code>a</code> as a local variable.</p>
<p>When running this line it first executes the “test” expression, <code>a = 0.zero?</code>.</p>
<p>Since the test is true it executes the “then” expression, <code>p a</code>. Since the <code>a</code> in the body was recorded as a method which does not exist the NameError is raised.</p>
<p>The same is true for <code>unless</code>.</p>
<h2 id="label-case+Expression"><code>case</code> Expression</h2>
<p>The <code>case</code> expression can be used in two ways.</p>
<p>The most common way is to compare an object against multiple patterns. The patterns are matched using the <tt>===</tt> method which is aliased to <tt>==</tt> on Object. Other classes must override it to give meaningful behavior. See Module#=== and Regexp#=== for examples.</p>
<p>Here is an example of using <code>case</code> to compare a String against a pattern:</p>
<pre class="code ruby"><code class="ruby"><span class='kw'>case</span> <span class='tstring'><span class='tstring_beg'>"</span><span class='tstring_content'>12345</span><span class='tstring_end'>"</span></span>
<span class='kw'>when</span> <span class='tstring'><span class='regexp_beg'>/</span><span class='tstring_content'>^1</span><span class='regexp_end'>/</span></span>
<span class='id identifier rubyid_puts'>puts</span> <span class='tstring'><span class='tstring_beg'>"</span><span class='tstring_content'>the string starts with one</span><span class='tstring_end'>"</span></span>
<span class='kw'>else</span>
<span class='id identifier rubyid_puts'>puts</span> <span class='tstring'><span class='tstring_beg'>"</span><span class='tstring_content'>I don't know what the string starts with</span><span class='tstring_end'>"</span></span>
<span class='kw'>end</span></code></pre>
<p>Here the string <code>"12345"</code> is compared with <code>/^1/</code> by calling <code>/^1/ === "12345"</code> which returns <code>true</code>. Like the <code>if</code> expression, the first <code>when</code> that matches is executed and all other matches are ignored.</p>
<p>If no matches are found, the <code>else</code> is executed.</p>
<p>The <code>else</code> and <code>then</code> are optional, this <code>case</code> expression gives the same result as the one above:</p>
<pre class="code ruby"><code class="ruby"><span class='kw'>case</span> <span class='tstring'><span class='tstring_beg'>"</span><span class='tstring_content'>12345</span><span class='tstring_end'>"</span></span>
<span class='kw'>when</span> <span class='tstring'><span class='regexp_beg'>/</span><span class='tstring_content'>^1</span><span class='regexp_end'>/</span></span>
<span class='id identifier rubyid_puts'>puts</span> <span class='tstring'><span class='tstring_beg'>"</span><span class='tstring_content'>the string starts with one</span><span class='tstring_end'>"</span></span>
<span class='kw'>end</span></code></pre>
<p>You may place multiple conditions on the same <code>when</code>:</p>
<pre class="code ruby"><code class="ruby"><span class='kw'>case</span> <span class='tstring'><span class='tstring_beg'>"</span><span class='tstring_content'>2</span><span class='tstring_end'>"</span></span>
<span class='kw'>when</span> <span class='tstring'><span class='regexp_beg'>/</span><span class='tstring_content'>^1</span><span class='regexp_end'>/</span></span><span class='comma'>,</span> <span class='tstring'><span class='tstring_beg'>"</span><span class='tstring_content'>2</span><span class='tstring_end'>"</span></span>
<span class='id identifier rubyid_puts'>puts</span> <span class='tstring'><span class='tstring_beg'>"</span><span class='tstring_content'>the string starts with one or is '2'</span><span class='tstring_end'>"</span></span>
<span class='kw'>end</span></code></pre>
<p>Ruby will try each condition in turn, so first <code>/^1/ === "2"</code> returns <code>false</code>, then <code>"2" === "2"</code> returns <code>true</code>, so “the string starts with one or is ‘2’” is printed.</p>
<p>You may use <code>then</code> after the <code>when</code> condition. This is most frequently used to place the body of the <code>when</code> on a single line.</p>
<pre class="code ruby"><code class="ruby">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</code></pre>
<p>The other way to use a <code>case</code> expression is like an if-elsif expression:</p>
<pre class="code ruby"><code class="ruby"><span class='id identifier rubyid_a'>a</span> <span class='op'>=</span> <span class='int'>2</span>
<span class='kw'>case</span>
<span class='kw'>when</span> <span class='id identifier rubyid_a'>a</span> <span class='op'>==</span> <span class='int'>1</span><span class='comma'>,</span> <span class='id identifier rubyid_a'>a</span> <span class='op'>==</span> <span class='int'>2</span>
<span class='id identifier rubyid_puts'>puts</span> <span class='tstring'><span class='tstring_beg'>"</span><span class='tstring_content'>a is one or two</span><span class='tstring_end'>"</span></span>
<span class='kw'>when</span> <span class='id identifier rubyid_a'>a</span> <span class='op'>==</span> <span class='int'>3</span>
<span class='id identifier rubyid_puts'>puts</span> <span class='tstring'><span class='tstring_beg'>"</span><span class='tstring_content'>a is three</span><span class='tstring_end'>"</span></span>
<span class='kw'>else</span>
<span class='id identifier rubyid_puts'>puts</span> <span class='tstring'><span class='tstring_beg'>"</span><span class='tstring_content'>I don't know what a is</span><span class='tstring_end'>"</span></span>
<span class='kw'>end</span></code></pre>
<p>Again, the <code>then</code> and <code>else</code> are optional.</p>
<p>The result value of a <code>case</code> expression is the last value executed in the expression.</p>
<h2 id="label-while+Loop"><code>while</code> Loop</h2>
<p>The <code>while</code> loop executes while a condition is true:</p>
<pre class="code ruby"><code class="ruby"><span class='id identifier rubyid_a'>a</span> <span class='op'>=</span> <span class='int'>0</span>
<span class='kw'>while</span> <span class='id identifier rubyid_a'>a</span> <span class='op'><</span> <span class='int'>10</span> <span class='kw'>do</span>
<span class='id identifier rubyid_p'>p</span> <span class='id identifier rubyid_a'>a</span>
<span class='id identifier rubyid_a'>a</span> <span class='op'>+=</span> <span class='int'>1</span>
<span class='kw'>end</span>
<span class='id identifier rubyid_p'>p</span> <span class='id identifier rubyid_a'>a</span></code></pre>
<p>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.</p>
<p>The <code>do</code> keyword is optional. The following loop is equivalent to the loop above:</p>
<pre class="code ruby"><code class="ruby"><span class='kw'>while</span> <span class='id identifier rubyid_a'>a</span> <span class='op'><</span> <span class='int'>10</span>
<span class='id identifier rubyid_p'>p</span> <span class='id identifier rubyid_a'>a</span>
<span class='id identifier rubyid_a'>a</span> <span class='op'>+=</span> <span class='int'>1</span>
<span class='kw'>end</span></code></pre>
<p>The result of a <code>while</code> loop is <code>nil</code> unless <code>break</code> is used to supply a value.</p>
<h2 id="label-until+Loop"><code>until</code> Loop</h2>
<p>The <code>until</code> loop executes while a condition is false:</p>
<pre class="code ruby"><code class="ruby"><span class='id identifier rubyid_a'>a</span> <span class='op'>=</span> <span class='int'>0</span>
<span class='kw'>until</span> <span class='id identifier rubyid_a'>a</span> <span class='op'>></span> <span class='int'>10</span> <span class='kw'>do</span>
<span class='id identifier rubyid_p'>p</span> <span class='id identifier rubyid_a'>a</span>
<span class='id identifier rubyid_a'>a</span> <span class='op'>+=</span> <span class='int'>1</span>
<span class='kw'>end</span>
<span class='id identifier rubyid_p'>p</span> <span class='id identifier rubyid_a'>a</span></code></pre>
<p>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.</p>
<p>Like a <code>while</code> loop, the <code>do</code> is optional.</p>
<p>Like a <code>while</code> loop, the result of an <code>until</code> loop is nil unless <code>break</code> is used.</p>
<h2 id="label-for+Loop"><code>for</code> Loop</h2>
<p>The <code>for</code> loop consists of <code>for</code> followed by a variable to contain the iteration argument followed by <code>in</code> and the value to iterate over using #each. The <code>do</code> is optional:</p>
<pre class="code ruby"><code class="ruby"><span class='kw'>for</span> <span class='id identifier rubyid_value'>value</span> <span class='kw'>in</span> [<span class='int'>1</span><span class='comma'>,</span> <span class='int'>2</span><span class='comma'>,</span> <span class='int'>3</span>] <span class='kw'>do</span>
<span class='id identifier rubyid_puts'>puts</span> <span class='id identifier rubyid_value'>value</span>
<span class='kw'>end</span></code></pre>
<p>Prints 1, 2 and 3.</p>
<p>Like <code>while</code> and <code>until</code>, the <code>do</code> is optional.</p>
<p>The <code>for</code> loop is similar to using #each, but does not create a new variable scope.</p>
<p>The result value of a <code>for</code> loop is the value iterated over unless <code>break</code> is used.</p>
<p>The <code>for</code> loop is rarely used in modern ruby programs.</p>
<h2 id="label-Modifier+while+and+until">Modifier <code>while</code> and <code>until</code></h2>
<p>Like <code>if</code> and <code>unless</code>, <code>while</code> and <code>until</code> can be used as modifiers:</p>
<pre class="code ruby"><code class="ruby"><span class='id identifier rubyid_a'>a</span> <span class='op'>=</span> <span class='int'>0</span>
<span class='id identifier rubyid_a'>a</span> <span class='op'>+=</span> <span class='int'>1</span> <span class='kw'>while</span> <span class='id identifier rubyid_a'>a</span> <span class='op'><</span> <span class='int'>10</span>
<span class='id identifier rubyid_p'>p</span> <span class='id identifier rubyid_a'>a</span> <span class='comment'># prints 10</span></code></pre>
<p><code>until</code> used as a modifier:</p>
<pre class="code ruby"><code class="ruby"><span class='id identifier rubyid_a'>a</span> <span class='op'>=</span> <span class='int'>0</span>
<span class='id identifier rubyid_a'>a</span> <span class='op'>+=</span> <span class='int'>1</span> <span class='kw'>until</span> <span class='id identifier rubyid_a'>a</span> <span class='op'>></span> <span class='int'>10</span>
<span class='id identifier rubyid_p'>p</span> <span class='id identifier rubyid_a'>a</span> <span class='comment'># prints 11</span></code></pre>
<p>You can use <code>begin</code> and <code>end</code> to create a <code>while</code> loop that runs the body once before the condition:</p>
<pre class="code ruby"><code class="ruby"><span class='id identifier rubyid_a'>a</span> <span class='op'>=</span> <span class='int'>0</span>
<span class='kw'>begin</span>
<span class='id identifier rubyid_a'>a</span> <span class='op'>+=</span> <span class='int'>1</span>
<span class='kw'>end</span> <span class='kw'>while</span> <span class='id identifier rubyid_a'>a</span> <span class='op'><</span> <span class='int'>10</span>
<span class='id identifier rubyid_p'>p</span> <span class='id identifier rubyid_a'>a</span> <span class='comment'># prints 10</span></code></pre>
<p>If you don’t use <code>rescue</code> or <code>ensure</code>, Ruby optimizes away any exception handling overhead.</p>
<h2 id="label-break+Statement"><code>break</code> Statement</h2>
<p>Use <code>break</code> to leave a block early. This will stop iterating over the items in <code>values</code> if one of them is even:</p>
<pre class="code ruby"><code class="ruby"><span class='id identifier rubyid_values'>values</span>.<span class='id identifier rubyid_each'>each</span> <span class='kw'>do</span> <span class='op'>|</span><span class='id identifier rubyid_value'>value</span><span class='op'>|</span>
<span class='kw'>break</span> <span class='kw'>if</span> <span class='id identifier rubyid_value'>value</span>.<span class='id identifier rubyid_even?'>even?</span>
<span class='comment'># ...
</span><span class='kw'>end</span></code></pre>
<p>You can also terminate from a <code>while</code> loop using <code>break</code>:</p>
<pre class="code ruby"><code class="ruby"><span class='id identifier rubyid_a'>a</span> <span class='op'>=</span> <span class='int'>0</span>
<span class='kw'>while</span> <span class='kw'>true</span> <span class='kw'>do</span>
<span class='id identifier rubyid_p'>p</span> <span class='id identifier rubyid_a'>a</span>
<span class='id identifier rubyid_a'>a</span> <span class='op'>+=</span> <span class='int'>1</span>
<span class='kw'>break</span> <span class='kw'>if</span> <span class='id identifier rubyid_a'>a</span> <span class='op'><</span> <span class='int'>10</span>
<span class='kw'>end</span>
<span class='id identifier rubyid_p'>p</span> <span class='id identifier rubyid_a'>a</span></code></pre>
<p>This prints the numbers 0 and 1.</p>
<p><code>break</code> accepts a value that supplies the result of the expression it is “breaking” out of:</p>
<pre class="code ruby"><code class="ruby"><span class='id identifier rubyid_result'>result</span> <span class='op'>=</span> [<span class='int'>1</span><span class='comma'>,</span> <span class='int'>2</span><span class='comma'>,</span> <span class='int'>3</span>].<span class='id identifier rubyid_each'>each</span> <span class='kw'>do</span> <span class='op'>|</span><span class='id identifier rubyid_value'>value</span><span class='op'>|</span>
<span class='kw'>break</span> <span class='id identifier rubyid_value'>value</span> <span class='op'>*</span> <span class='int'>2</span> <span class='kw'>if</span> <span class='id identifier rubyid_value'>value</span>.<span class='id identifier rubyid_even?'>even?</span>
<span class='kw'>end</span>
<span class='id identifier rubyid_p'>p</span> <span class='id identifier rubyid_result'>result</span> <span class='comment'># prints 4</span></code></pre>
<h2 id="label-next+Statement"><code>next</code> Statement</h2>
<p>Use <code>next</code> to skip the rest of the current iteration:</p>
<pre class="code ruby"><code class="ruby"><span class='id identifier rubyid_result'>result</span> <span class='op'>=</span> [<span class='int'>1</span><span class='comma'>,</span> <span class='int'>2</span><span class='comma'>,</span> <span class='int'>3</span>].<span class='id identifier rubyid_map'>map</span> <span class='kw'>do</span> <span class='op'>|</span><span class='id identifier rubyid_value'>value</span><span class='op'>|</span>
<span class='kw'>next</span> <span class='kw'>if</span> <span class='id identifier rubyid_value'>value</span>.<span class='id identifier rubyid_even?'>even?</span>
<span class='id identifier rubyid_value'>value</span> <span class='op'>*</span> <span class='int'>2</span>
<span class='kw'>end</span>
<span class='id identifier rubyid_p'>p</span> <span class='id identifier rubyid_result'>result</span> <span class='comment'># prints [2, nil, 6]</span></code></pre>
<p><code>next</code> accepts an argument that can be used as the result of the current block iteration:</p>
<pre class="code ruby"><code class="ruby"><span class='id identifier rubyid_result'>result</span> <span class='op'>=</span> [<span class='int'>1</span><span class='comma'>,</span> <span class='int'>2</span><span class='comma'>,</span> <span class='int'>3</span>].<span class='id identifier rubyid_map'>map</span> <span class='kw'>do</span> <span class='op'>|</span><span class='id identifier rubyid_value'>value</span><span class='op'>|</span>
<span class='kw'>next</span> <span class='id identifier rubyid_value'>value</span> <span class='kw'>if</span> <span class='id identifier rubyid_value'>value</span>.<span class='id identifier rubyid_even?'>even?</span>
<span class='id identifier rubyid_value'>value</span> <span class='op'>*</span> <span class='int'>2</span>
<span class='kw'>end</span>
<span class='id identifier rubyid_p'>p</span> <span class='id identifier rubyid_result'>result</span> <span class='comment'># prints [2, 2, 6]</span></code></pre>
<h2 id="label-redo+Statement"><code>redo</code> Statement</h2>
<p>Use <code>redo</code> to redo the current iteration:</p>
<pre class="code ruby"><code class="ruby"><span class='id identifier rubyid_result'>result</span> <span class='op'>=</span> []
<span class='kw'>while</span> <span class='id identifier rubyid_result'>result</span>.<span class='id identifier rubyid_length'>length</span> <span class='op'><</span> <span class='int'>10</span> <span class='kw'>do</span>
<span class='id identifier rubyid_result'>result</span> <span class='op'><<</span> <span class='id identifier rubyid_result'>result</span>.<span class='id identifier rubyid_length'>length</span>
<span class='kw'>redo</span> <span class='kw'>if</span> <span class='id identifier rubyid_result'>result</span>.<span class='id identifier rubyid_last'>last</span>.<span class='id identifier rubyid_even?'>even?</span>
<span class='id identifier rubyid_result'>result</span> <span class='op'><<</span> <span class='id identifier rubyid_result'>result</span>.<span class='id identifier rubyid_length'>length</span> <span class='op'>+</span> <span class='int'>1</span>
<span class='kw'>end</span>
<span class='id identifier rubyid_p'>p</span> <span class='id identifier rubyid_result'>result</span></code></pre>
<p>This prints [0, 1, 3, 3, 5, 5, 7, 7, 9, 9, 11]</p>
<p>In Ruby 1.8, you could also use <code>retry</code> where you used <code>redo</code>. This is no longer true, now you will receive a SyntaxError when you use <code>retry</code> outside of a <code>rescue</code> block. See <a href="file.exceptions.html" title="Exceptions">Exceptions</a> for proper usage of <code>retry</code>.</p>
<h2 id="label-Flip-Flop">Flip-Flop</h2>
<p>The flip-flop is a rarely seen conditional expression. It’s primary use is for processing text from ruby one-line programs used with <code>ruby -n</code> or <code>ruby -p</code>.</p>
<p>The form of the flip-flop is an expression that indicates when the flip-flop turns on, <code>..</code> (or <code>...</code>), then an expression that indicates when the flip-flop will turn off. While the flip-flop is on it will continue to evaluate to <code>true</code>, and <code>false</code> when off.</p>
<p>Here is an example:</p>
<pre class="code ruby"><code class="ruby"><span class='id identifier rubyid_selected'>selected</span> <span class='op'>=</span> []
<span class='int'>0</span>.<span class='id identifier rubyid_upto'>upto</span> <span class='int'>10</span> <span class='kw'>do</span> <span class='op'>|</span><span class='id identifier rubyid_value'>value</span><span class='op'>|</span>
<span class='id identifier rubyid_selected'>selected</span> <span class='op'><<</span> <span class='id identifier rubyid_value'>value</span> <span class='kw'>if</span> <span class='id identifier rubyid_value'>value</span><span class='op'>==</span><span class='int'>2</span><span class='op'>..</span><span class='id identifier rubyid_value'>value</span><span class='op'>==</span><span class='int'>8</span>
<span class='kw'>end</span>
<span class='id identifier rubyid_p'>p</span> <span class='id identifier rubyid_selected'>selected</span> <span class='comment'># prints [2, 3, 4, 5, 6, 7, 8]</span></code></pre>
<p>In the above example, the on condition is <code>n==2</code>. The flip-flop is initially off (false) for 0 and 1, but becomes on (true) for 2 and remains on through 8. After 8 it turns off and remains off for 9 and 10.</p>
<p>The flip-flop must be used inside a conditional such as <code>if</code>, <code>while</code>, <code>unless</code>, <code>until</code> etc. including the modifier forms.</p>
<p>When you use an inclusive range (<code>..</code>), the off condition is evaluated when the on condition changes:</p>
<pre class="code ruby"><code class="ruby"><span class='id identifier rubyid_selected'>selected</span> <span class='op'>=</span> []
<span class='int'>0</span>.<span class='id identifier rubyid_upto'>upto</span> <span class='int'>5</span> <span class='kw'>do</span> <span class='op'>|</span><span class='id identifier rubyid_value'>value</span><span class='op'>|</span>
<span class='id identifier rubyid_selected'>selected</span> <span class='op'><<</span> <span class='id identifier rubyid_value'>value</span> <span class='kw'>if</span> <span class='id identifier rubyid_value'>value</span><span class='op'>==</span><span class='int'>2</span><span class='op'>..</span><span class='id identifier rubyid_value'>value</span><span class='op'>==</span><span class='int'>2</span>
<span class='kw'>end</span>
<span class='id identifier rubyid_p'>p</span> <span class='id identifier rubyid_selected'>selected</span> <span class='comment'># prints [2]</span></code></pre>
<p>Here, both sides of the flip-flop are evaluated so the flip-flop turns on and off only when <code>value</code> equals 2. Since the flip-flop turned on in the iteration it returns true.</p>
<p>When you use an exclusive range (<code>...</code>), the off condition is evaluated on the following iteration:</p>
<pre class="code ruby"><code class="ruby"><span class='id identifier rubyid_selected'>selected</span> <span class='op'>=</span> []
<span class='int'>0</span>.<span class='id identifier rubyid_upto'>upto</span> <span class='int'>5</span> <span class='kw'>do</span> <span class='op'>|</span><span class='id identifier rubyid_value'>value</span><span class='op'>|</span>
<span class='id identifier rubyid_selected'>selected</span> <span class='op'><<</span> <span class='id identifier rubyid_value'>value</span> <span class='kw'>if</span> <span class='id identifier rubyid_value'>value</span><span class='op'>==</span><span class='int'>2</span><span class='op'>...</span><span class='id identifier rubyid_value'>value</span><span class='op'>==</span><span class='int'>2</span>
<span class='kw'>end</span>
<span class='id identifier rubyid_p'>p</span> <span class='id identifier rubyid_selected'>selected</span> <span class='comment'># prints [2, 3, 4, 5]</span></code></pre>
<p>Here, the flip-flop turns on when <code>value</code> equals 2, but doesn’t turn off on the same iteration. The off condition isn’t evaluated until the following iteration and <code>value</code> will never be two again.</p>
<div id='footer'></div>
</div> <!-- content -->
</div> <!-- y_main -->
</body>
</html>