The following code works fine:
person = {:a=>:A, :b=>:B, :c=>:C}
berson = {:a=>:A1, :b=>:B1, :c=>:C1}
kerson = person.merge(berson) do | key, oldv, newv |
if key == :a
oldv
elsif key == :b
newv
else
key
end
end
puts kerson.inspect
but if I add the return keyword inside the "if block", I get an error:
person = {:a=>:A, :b=>:B, :c=>:C}
berson = {:a=>:A1, :b=>:B1, :c=>:C1}
kerson = person.merge(berson) do | key, oldv, newv |
if key == :a
return oldv
elsif key == :b
return newv
else
return key
end
end
puts kerson.inspect
The error from the above code is:
unexpected return (LocalJumpError)
Can anyone explain this? I thought the 'return' keyword could be optionally used wherever there is already the assumption of a value being returned.