Created
August 12, 2014 15:21
-
-
Save kyletolle/26d1405c02e89e7f889d to your computer and use it in GitHub Desktop.
Hash Modification Example
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# | |
# 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