Passing Blocks (Not Procs) into Methods

OK, so this has been more theoretically cool than actually cool, partly because this is all a bit of a hassle to use. I’m man enough to admit that. A lot of the problem is that we have to go through three steps (defining the method, making the proc, and calling the method with the proc) when it sort of feels like there should be only two (defining the method and passing the block of code right into the method, without using a proc at all), since most of the time you don’t want to use the proc/block after you pass it into the method.

It should be…more like how iterators work! Sho’ nuff, baby:

class​ Array
def​ each_even(&was_a_block__now_a_proc)
# We start with "true" because
# arrays start with 0, ...

Get Learn to Program, 2nd 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.