Passing Blocks (Not Procs) into Methods

Okay, so this has been more theoretically cool than actually cool, partly because this is all a bit of a hassle to use. I can admit that. A lot of the problem is that you have to go through three steps (defining the method, making the proc, and calling the method with the proc) instead of only two (defining the method and passing the block of code right into the method, without using a proc at all), since you usually won’t want to use the proc/block after you pass it into the method.

It should be…more like how iterators work. And you know what? It is. In this example, you’ll create a new array iterator, like each, but instead of visiting each element in the array, you only look at every other element, ...

Get Learn to Program, 3rd Edition now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.