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