Skip to content

Instantly share code, notes, and snippets.

@kyletolle
Created August 12, 2014 15:21
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save kyletolle/26d1405c02e89e7f889d to your computer and use it in GitHub Desktop.
Save kyletolle/26d1405c02e89e7f889d to your computer and use it in GitHub Desktop.
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