Information Technology Reference
In-Depth Information
REMEMBERING THE ORDER
It's easy to forget the order of the equal-tilde matcher. Is it =~ or ~= ? The easiest way to
always get the order correct is to think in alphabetical order. “Equals” comes before
“tilde” in the dictionary, so the equal sign comes before the tilde in the expression.
For example, you could try using regular expressions to find names in a list of players on a
baseball team beginning with a certain letter of the alphabet. Regular expressions can be
anchored using the caret ( ^ ), which starts a match at the beginning of the string. The follow-
ing snippet searches for all players that have a last name beginning with the letter F :
players . select ddo | name , statistics |
name =~ /^F/
end
end
Conditionals and Flow
Like most programming languages, Ruby supports conditionals to manage the flow and con-
trol of application logic. The most common control gate is the if keyword. The if keyword
is complemented by the unless keyword, which is the equivalent of if not :
# Using if
iif some_condition
puts "happened"
else
else
puts "didn't happen"
end
end
# Using unless
unless
unless some_condition
puts "didn't happen"
else
else
puts "happened"
end
end
It is also possible to have multiple levels of conditionals using the elsif keyword:
Search WWH ::




Custom Search