small medium large xlarge

29 Aug 2015, 15:04
Brandon Gresham (1 post)

Disclaimer: I’m not a Rubyist, but Ruby interests me so I’m trying to use this book to learn it better. I DID try and google this question to the best of my Google-foo, but to no avail.

In Chapter 2 I see the iterators being used via underscores instead of periods – yet I don’t seem to find any Ruby reference outside of this book that does this. It works, so I guess it’s legit, but could someone provide me a link to explain it better?


each_cell do |cell|

Thank you!

29 Aug 2015, 19:03
Jamis Buck (30 posts)

Hello Brandon! I’m sorry for the confusion, there. That each_cell call that you’re seeing is actually a custom iterator, implemented as a method on the Grid class. In that method, you’ll see a call to yield – what this does is invoke the block of code that was attached when the each_cell method was called.

(If you look at each_cell, you’ll see that it actually calls another custom iterator: each_row, which in turn calls the standard each method on the underlying grid array itself.)

I hope that clarifies things a bit. It’s a pretty standard Ruby idiom, but if you’re not used to Ruby’s way of doing things, these iterators can certainly be a bit confusing!

You must be logged in to comment