Last active
August 29, 2015 14:13
-
-
Save vasilakisfil/3a673fa53127a801e571 to your computer and use it in GitHub Desktop.
Ruby hashes inconsistency
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
# copy and store in another variable: | |
[16] pry(main)> hash1 = {a: 'asdasd', b: 'asdasd'} | |
=> {:a=>"asdasd", :b=>"asdasd"} | |
[17] pry(main)> hash2 = hash1 | |
=> {:a=>"asdasd", :b=>"asdasd"} | |
[18] pry(main)> hash2.delete(:a) | |
=> "asdasd" | |
[19] pry(main)> hash2 | |
=> {:b=>"asdasd"} | |
[20] pry(main)> hash1 | |
=> {:b=>"asdasd"} | |
[21] pry(main)> | |
#shallow copy | |
[23] pry(main)> hash3 = {a: 'asdasd', b: 'asdasd'} | |
=> {:a=>"asdasd", :b=>"asdasd"} | |
[24] pry(main)> hash4 = hash3.clone | |
=> {:a=>"asdasd", :b=>"asdasd"} | |
[25] pry(main)> hash3.delete(:a) | |
=> "asdasd" | |
[26] pry(main)> hash3 | |
=> {:b=>"asdasd"} | |
[27] pry(main)> hash4 | |
=> {:a=>"asdasd", :b=>"asdasd"} | |
[28] pry(main)> hash3[:b] << ' something else' | |
=> "asdasd something else" | |
[29] pry(main)> hash3 | |
=> {:b=>"asdasd something else"} | |
[30] pry(main)> hash4 | |
=> {:a=>"asdasd", :b=>"asdasd something else"} | |
[31] pry(main)> | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
here we see Ruby failing in multiple ways