Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Hash Modification Example
#
# Hashes can be modified by a method they're passed in to, which would affect
# what they contain. This could be an unexpected side-effect.
# $ ruby hash-modification.rb
# Hash before modification:
# {:a=>1, :b=>2}
# Hash after modification
# {:a=>1, :b=>2, :c=>3, :d=>4}
#
original_hash = { a: 1, b: 2 }
puts "Hash before modification:"
puts original_hash
def hash_modifier(hash)
hash.merge!(c: 3, d: 4)
end
hash_modifier(original_hash)
puts "Hash after modification"
puts original_hash
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.