I am not able to understand the logic of this code:
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"
How is the object
vf.inject(:+)
calling the methodeach
in this program?How does the
each
method work in this program, because there is no block argument mentioned in function definition?If I simply call
vf.each
, why am I getting the following error?vowel_finder.rb:8:in `block in each': no block given (yield) (LocalJumpError) from vowel_finder.rb:8:in `scan' from vowel_finder.rb:8:in `each' from vowel_finder.rb:13:in `<main>'
One of the few things that I understood is that the each
method in this class overrides the each
method from the included Enumerable
module. Other than that I did not understand anything about each
and blocks.
Could someone please explain me the logic and how it works internally?