BC Blog

Chase Excellence, Success will Follow

0%

Attaching Ruby Enumerators to Objects

While reading “The Well-Grounded Rubyist”, I found this topic quite interesting.

1
2
names = %w{ David Black Yukihiro Matsumoto }
e = names.enum_for(:select)

I could call enum_for on the object from which I want the enumerator to draw its iterations.

1
e.each { |n| n.include?('a') } #=> ["David", "Black", "Matsumoto"]

By calling upon .each method, I was able to allow the iterator to serve as a kind of front end to array’s select.

Welcome to my other publishing channels