Here is a code sample from the ruby pickaxe book:
Class VowelFinder
include Enumerable
def initialize(string)
@string = string
end
def each
@string.scan(/[aeiou]/] do |vowel|
yield vowel
end
end
end
vf = VowelFinder.new("the quick brown fox jumped")
vf.inject(:+) # => 'euiooue'
What I am having a hard time understanding is where the modified ‘each’ method comes into play? I assume inject is calling it at some point but don’t understand why it’s doing it, and how I could predict or emulate this behavior in my code.
Thanks!
the
VowelFinderclass implements the protocol that is mandatory for theEnumerablemodule that you can include to gain a lot of ruby iterator methods: http://apidock.com/ruby/Enumerable