Block Return Values
Learn about block return values.
We'll cover the following...
Remember when we learned that a block returns a value just like methods do? So far, in our two previous examples, we didn’t do anything with the block’s return values.
An example using collect
Here’s an example that does that:
This takes the array of numbers and transforms it into another array.
Explanation
-
It does this by calling the
collectmethod on the original array, which executes the given block for each element and collects each array return value returned by the block. The resulting array is then returned by thecollectmethod and printed to the screen. -
In other words, the
collectmethod uses the block as a transformer. It takes each element of the array, passes it to the block to transform it to something else, and then keeps all transformed values in a new array that thecollectmethod eventually returns.
Note that the collect method has an alias, which is map. These are exactly the same methods. Many programmers prefer map over collect because it’s shorter and more commonly used in other languages. That said, collect is used here because it simply expresses what the method does more clearly.
Remember: The
collectandmapmethods can be used to transform an array into another array.
An example using select
Here’s another example that uses the return value of the block:
In this case, the select method uses the block as a filter, or criterion, to select values out of the array and then return a new array with the selected values.
A step-by-step explanation
-
We create an array,
[1, 2, 3, 4, 5]. -
We then call the
selectmethod on it and pass our block. -
Now, the
selectmethod executes our block for each number. -
It first executes the block, passing the number
1as an argument. -
We’re now inside the block, and a local
numbervariable is assigned to the object that was passed as an argument, which is the number1. -
We call the
odd?method on this number in our block and because1is odd, it returnstrue. -
Because this is the only, and therefore last, statement in the body of our block, it returns
trueto theselectmethod. The select method therefore keeps (selects) the number1. -
It then executes the block again, this time passing the number
2. Because this isn’t an odd number, theodd?method, and therefore our block, returnsfalseback to theselectmethod. It then discards this element. -
It keeps doing this for each remaining element in the array, and it eventually has the
[1, 3, 5]array. The block serves as filtering criteria. -
The
selectmethod then returns this array, and Ruby passes it to thepmethod, which prints the array out to the screen.
Therefore, the code above prints out [1, 3, 5].
Remember: The
selectmethod can be used to select a new array with values that match block-defined criteria.
An example using detect
Here’s another example of a method that uses the block as a criterion:
Again, detect passes each element of the array to the block, one by one, and checks its return value. However, as soon as the block returns something true, the detect method returns the current object itself. Therefore, this prints out 2, the first even number in the array.