Life Without Iterators
Let's play dumb for a while: Suppose we've never seen iterators, and we would like to give an object foo a completely new task to do. We mean this in a very general sense; although the object has its own methods, which are the tricks it already knows how to perform, we'd like to be able to tell it to do something arbitrary and unfamiliar. In other words, we'd like to pass it some code. How can we teach an old dog a new trick?
Here's a little game to make the problem a little more concrete. Secretive is a class whose objects each own a number @r chosen randomly somewhere between 0 and 1.
Note
That's strictly less than 1, although possibly equal to 0. (It will probably take you a lot of tries before you see it come out to ...
Get Sams Teach Yourself Ruby in 21 Days 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.