Blocks: Ruby’s Secret Sauce
Blocks are like anonymous methods:
# Print out a list of of people from
# each person in the Array
people
.
each
do
|
person
|
puts
"* #{
person
.
name
}"
end
# A block using the bracket syntax
5
.
times
{
puts
"Ruby rocks!"
}
# Custom sorting
[
2
,
1
,
3
]
.
sort!
{
|
a
,
b
|
b
<=>
a
}