O'Reilly logo

Sams Teach Yourself Ruby in 21 Days by Mark Slagell

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

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 ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required