Information Technology Reference
In-Depth Information
iif false
puts "this can't possibly happen"
elsif
elsif nil
puts "this won't happen either"
elsif
elsif true
puts "this will definitely happen"
else
else
puts "this won't happen, because the method is short-circuited
end
If the result of an expression is falsey, the body of that block will not be executed.
ni l is considered a falsey value, so this block will not be evaluated.
Be cause the former elsif block will always execute, this else block is unnecessary.
Less common in Ruby (but common in Chef) is the case statement. Very similar to the
if statement, the case statement applies more syntactic sugar and logic for the deve-
loper:
case
case some_condition
when
when "literal string"
# ...
when
when /regular expression/
# ...
when
when list , of , items
# ...
else
else
# ...
end
end
If a literal object is supplied, the case statement will perform a pure equality match.
If a regular expression is supplied, Ruby will attempt to call match on the receiving ob-
ject.
If multiple items are given, they are interpreted as “or” (if the item matches “list”, “of”,
or “items”).
 
 
 
 
 
 
 
 
Search WWH ::




Custom Search