In regards to adding an key => value
pair to an existing populated hash in Ruby, I\'m in the process of working through Apress\' Beginning Ruby and have just fi
hash = { a: 'a', b: 'b' }
=> {:a=>"a", :b=>"b"}
hash.merge({ c: 'c', d: 'd' })
=> {:a=>"a", :b=>"b", :c=>"c", :d=>"d"}
Returns the merged value.
hash
=> {:a=>"a", :b=>"b"}
But doesn't modify the caller object
hash = hash.merge({ c: 'c', d: 'd' })
=> {:a=>"a", :b=>"b", :c=>"c", :d=>"d"}
hash
=> {:a=>"a", :b=>"b", :c=>"c", :d=>"d"}
Reassignment does the trick.