Sometimes when I’m fumbling around in irb on an API I don’t know, or with Active Record queries, I get an Enumerator object back when I don’t want to. So if I save this Enumerator as a variable, what the heck do I do with it? And why do I get an Enumerator anyway?
So what we’ll do is play around with a hash of elements. Elements have a name, color and what happens when you touch them attributes. We’ll iterate through them and print out the values as normal but we’ll also store the Enumerator by itself and iterate through it later.
Run this and you get:
What do you use a Ruby enumerator for?
#<Enumerator:0x00000100859c28></p>
You don't get an enumerator if you pass a block in:
{:name=>"wind", :color=>"white", :touch=>"I'm flying."}
Delayed enum search:
I'm dirty., I'm flying., I'm on fire!, I'm wet., Brr!
Standard collect with no enumerator:
brown,white,red,blue,blue
Nice hash associating element to color:
{"earth"=>"brown", "wind"=>"white", "fire"=>"red", "water"=>"blue", "ice"=>"blue"}
Very simple example but for me, this was confusing and helped me to understand that many things can return an Enumerator and many methods can work with an Enumerator. But what we’re doing here is just really seeing that the find method returns an Enumerator. What if we want to do something more original?
Let’s create a class that mixes-in the Enumerable module.
I’ve included the output in the comments above. We define our stages of life and include a value 0-7 for each stage. I used a value in the @stages hash as the sort key so we could implement a custom sort for the #max and #min methods. Also notice that we have to sort our hash by value in #each because otherwise our hash would have to be defined in order (ie: nothing, egg, baby …).
So there you go, a very basic tour of Enumerable in Ruby. If this is interesting also look at how to mixin and implement your own Comparable.