My_inject - "expected given block to yield control once but yielded twice"

I am working on the rspec2 03_iteration.rb “my_inject” problem.
The solution has the accumulator start at the first value, below:

def my_inject(&blk)
val = self.first
self.drop(1).my_each { |el| val = blk.call(val, el) }
val
end

But my_inject works as expected in repl if you just start the accumulator at 0:

def my_inject(&prc)
val = 0
self.each do |el| val = prc.call(val, el)
end
val
end

#[1,2,3].my_inject {|sum, num| sum += num}
=> 6

However, starting at zero with the code above results in the following error in rspec:

Array#my_inject calls the block passed to it
Failure/Error: expect do |block|
expected given block to yield control once but yielded twice
# ./spec/03_iteration_spec.rb:182:in `block (3 levels) in <top (required)>’

What does “expected given block to yield control once but yielded twice” mean?

1 Like