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 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639
|
= 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.
== Ternary if
You may also write a if-then-else expression using <code>?</code> and
<code>:</code>. This ternary if:
input_type = gets =~ /hello/i ? "greeting" : "other"
Is the same as this +if+ expression:
input_type =
if gets =~ /hello/i
"greeting"
else
"other"
end
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.
== +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" statement 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" statement, 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 <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.
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.
Since Ruby 2.7, +case+ expressions also provide a more powerful
pattern matching feature via the +in+ keyword:
case {a: 1, b: 2, c: 3}
in a: Integer => m
"matched: #{m}"
else
"not matched"
end
# => "matched: 1"
The pattern matching syntax is described on
{its own page}[rdoc-ref:syntax/pattern_matching.rdoc].
== +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 as 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+.
== Modifier Statements
Ruby's grammar differentiates between statements and expressions. All
expressions are statements (an expression is a type of statement), but
not all statements are expressions. Some parts of the grammar accept
expressions and not other types of statements, which causes code that
looks similar to be parsed differently.
For example, when not used as a modifier, +if+, +else+, +while+, +until+,
and +begin+ are expressions (and also statements). However, when
used as a modifier, +if+, +else+, +while+, +until+ and +rescue+
are statements but not expressions.
if true; 1 end # expression (and therefore statement)
1 if true # statement (not expression)
Statements that are not expressions cannot be used in contexts where an
expression is expected, such as method arguments.
puts( 1 if true ) #=> SyntaxError
You can wrap a statement in parentheses to create an expression.
puts((1 if true)) #=> 1
If you put a space between the method name and opening parenthesis, you
do not need two sets of parentheses.
puts (1 if true) #=> 1, because of optional parentheses for method
This is because this is parsed similar to a method call without
parentheses. It is equivalent to the following code, without the creation
of a local variable:
x = (1 if true)
p x
In a modifier statement, the left-hand side must be a statement and the
right-hand side must be an expression.
So in <code>a if b rescue c</code>, because <code>b rescue c</code> is a
statement that is not an expression, and therefore is not allowed as the
right-hand side of the +if+ modifier statement, the code is necessarily
parsed as <code>(a if b) rescue c</code>.
This interacts with operator precedence in such a way that:
stmt if v = expr rescue x
stmt if v = expr unless x
are parsed as:
stmt if v = (expr rescue x)
(stmt if v = expr) unless x
This is because modifier +rescue+ has higher precedence than <code>=</code>,
and modifier +if+ has lower precedence than <code>=</code>.
== Flip-Flop
The flip-flop is a slightly special conditional expression. One of its
typical uses is processing text from ruby one-line programs used with
<code>ruby -n</code> or <code>ruby -p</code>.
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 +true+, and +false+ when off.
Here is an example:
selected = []
0.upto 10 do |value|
selected << value if value==2..value==8
end
p selected # prints [2, 3, 4, 5, 6, 7, 8]
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.
The flip-flop must be used inside a conditional such as <code>!</code>,
<code>? :</code>, +not+, +if+, +while+, +unless+, +until+ etc. including the
modifier forms.
When you use an inclusive range (<code>..</code>), the `off' condition is
evaluated when the `on' condition changes:
selected = []
0.upto 5 do |value|
selected << value if value==2..value==2
end
p selected # prints [2]
Here, both sides of the flip-flop are evaluated so the flip-flop turns on and
off only when +value+ equals 2. Since the flip-flop turned on in the
iteration it returns true.
When you use an exclusive range (<code>...</code>), the `off' condition is
evaluated on the following iteration:
selected = []
0.upto 5 do |value|
selected << value if value==2...value==2
end
p selected # prints [2, 3, 4, 5]
Here, the flip-flop turns on when +value+ equals 2, but doesn't turn off on the
same iteration. The `off' condition isn't evaluated until the following
iteration and +value+ will never be two again.
== throw/catch
+throw+ and +catch+ are used to implement non-local control flow in Ruby. They
operate similarly to exceptions, allowing control to pass directly from the
place where +throw+ is called to the place where the matching +catch+ is
called. The main difference between +throw+/+catch+ and the use of exceptions
is that +throw+/+catch+ are designed for expected non-local control flow,
while exceptions are designed for exceptional control flow situations, such
as handling unexpected errors.
When using +throw+, you provide 1-2 arguments. The first argument is the
value for the matching +catch+. The second argument is optional (defaults to
+nil+), and will be the value that +catch+ returns if there is a matching
+throw+ inside the +catch+ block. If no matching +throw+ method is called
inside a +catch+ block, the +catch+ method returns the return value of the
block passed to it.
def a(n)
throw :d, :a if n == 0
b(n)
end
def b(n)
throw :d, :b if n == 1
c(n)
end
def c(n)
throw :d if n == 2
end
4.times.map do |i|
catch(:d) do
a(i)
:default
end
end
# => [:a, :b, nil, :default]
If the first argument you pass to +throw+ is not handled by a matching
+catch+, an UncaughtThrowError exception will be raised. This is because
+throw+/+catch+ should only be used for expected control flow changes, so
using a value that is not already expected is an error.
+throw+/+catch+ are implemented as Kernel methods (Kernel#throw and
Kernel#catch), not as keywords. So they are not usable directly if you are
in a BasicObject context. You can use Kernel.throw and Kernel.catch in
this case:
BasicObject.new.instance_exec do
def a
b
end
def b
c
end
def c
::Kernel.throw :d, :e
end
result = ::Kernel.catch(:d) do
a
end
result # => :e
end
|