Remember that we said a block returns a value just like methods do? So far, in our two examples above, we did not do anything with the return values of the block.
Example
Here’s an example that does that:
This will take the array of numbers, and transform it into another array.
Explanation
-
It does this by calling the method
collecton the original array, which calls the given block for each of the elements and collects each of the return values returned by the block. The resulting array is then returned by the methodcollect, and printed to the screen. -
In other words, the method ...
Create a free account to access the full course.
By signing up, you agree to Educative's Terms of Service and Privacy Policy