Blocks and Block Parameters
In Ruby, the body of an iterator is called a block, and any variables declared between upright bars at the top of a block are called block parameters. In a way, a block works like a function, and the block parameters work like a function’s argument list. The each method runs the code inside the block and passes to it the arguments supplied by a collection (such as the array, multiarr). In the example from the previous section, the each method repeatedly passes an array of four elements to the block, and those elements initialize the four block parameters, a, b, c, d. Blocks can be used for other things, in addition to iterating over collections.
Ruby also has an alternative syntax for delimiting blocks. Instead of using ...
Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.
Read now
Unlock full access