Vahagn H. wrote in post #991914:
Thank you for your explanations, I can see now that :each is an
inherited method and it had to be ferreted out of the MatchData set of
methods.
I’m not sure what you are referring to because each() is neither defined
nor inherited in the MatchData class. Check the output of:
md = /h/.match(‘hello’)
puts md.methods.sort
And Enumerator defines its own each() method.
So in order to use an enumerator, an object itself has to define an
(:each) method and then an enumerator just augments that.
No, each() is not a requirement. An enumerator can be hooked up to any
“iteration method” for an object. An “iteration method” is any method
that calls yield(). Here is an example:
class Dog
def bark
yield ‘woof’
yield ‘whooooo’
yield ‘ruuuff’
end
end
dog = Dog.new
e = dog.enum_for(:bark)
results = e.select{|sound| sound[0] == ‘w’}
p results
–output:—
[“woof”, “whooooo”]
results = e.map{|sound| “#{sound.capitalize}” << ‘!’ }
p results
–output:–
[“Woof!”, “Whooooo!”, “Ruuuff!”]
if e.include?(‘woof’)
puts ‘yes’
end
–output:–
yes
You may be confusing Enumer-ator with something you read about
Enumer-able. If you want to make the Enumer-able methods available to
your class, e.g. group_by(), each_slice(), select(), map(), include?(),
etc., then your class has to:
- Define an each() method, and
- include Enumerable
Now the confusing part: the Enumer-ator class defines a few of its own
methods, but it also includes Enumer-able, so an enumerator object has
both the Enumerator and Enumerable methods available to it.
An enumerator can also be hooked up to a block that you define:
e = Enumerator.new do |y|
10.upto(14) do |num|
y << num
end
end
results = e.map{|x| x + 5}
p results
–output:–
[15, 16, 17, 18, 19]
That ‘y’ thing is a ruby object into which you insert(<<) values, and it
knows how to produce the values on command when the enumerator asks for
them.