Enumerable and Class Extensions Debugging, Rspec clarification: my_merge

Hello this is code written. It pasts all specs, but I am confused with following one rspec.

Rspec :
it ‘yields each element to the block’ do
test_hash_1 = { a: 10, b: 2 }
test_hash_2 = { b: 3, c: 4 }
expect { |b| test_hash_1.my_merge(test_hash_2, &b)}.to yield_successive_args([:b, 2, 3])
end

my_merge code:
def my_merge(hash2, &prc)
new_hash = self
if prc.nil?
hash2.each do |k2, v2|
new_hash[k2] = v2
end
elsif prc
self.each do |key, value|
debugger
new_hash[key]= hash2[key] ? prc.call(key, self[key], hash2[key]) : value
end

  hash2.each do |key, value|
    new_hash[key] = value  if new_hash[key].nil?
  end
end
new_hash

end

-Why is the answer [:b, 2, 3] ?
-Shouldn’t the answer at least have the other keys in it because of this line:
new_hash[key]= hash2[key] ? prc.call(key, self[key], hash2[key]) : value

Hi Henry,

If I’m understanding your question correctly, I believe you have a misconception about what this line in the spec says: expect { |b| test_hash_1.my_merge(test_hash_2, &b)}.to yield_successive_args([:b, 2, 3]). This line says that if you run test_hash_1.my_merge(test_hash_2, &b), the arguments :b, 2, and 3 will be passed to the block in that order. Here’s more information about this RSpec method. So this spec doesn’t actually test whether the method returns the right thing—that’s what the next spec is for.

Does this help?

Hi Matthias,

Thank you very much for your help. If I’m understanding correctly, the spec is asking if a block was ran on those arguments?

Hi Henry,

That’s correct!