Created
February 25, 2019 00:57
-
-
Save EmilReji/15fbf7002b46882b50064036288e6d90 to your computer and use it in GitHub Desktop.
Nested Data Structures Shallow Copy Question
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
arr1 = ["a", "b", "c"] | |
arr2 = arr1.dup | |
p arr1.object_id | |
p arr2.object_id | |
p arr1.object_id == arr2.object_id # false | |
p arr1[0].object_id | |
p arr2[0].object_id | |
p arr1[0].object_id == arr2[0].object_id # true | |
arr2.map! do |char| | |
char.upcase | |
end | |
p arr1 # => ["a", "b", "c"] | |
p arr2 # => ["A", "B", "C"] | |
p arr1.object_id | |
p arr2.object_id | |
p arr1.object_id == arr2.object_id # false | |
p arr1[0].object_id | |
p arr2[0].object_id | |
p arr1[0].object_id == arr2[0].object_id # false |
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
arr1 = ["a", "b", "c"] | |
arr2 = arr1.dup | |
p arr1.object_id | |
p arr2.object_id | |
p arr1.object_id == arr2.object_id # false | |
p arr1[0].object_id | |
p arr2[0].object_id | |
p arr1[0].object_id == arr2[0].object_id # true | |
arr2.map do |char| | |
char.upcase! | |
end | |
p arr1 # => ["A", "B", "C"] | |
p arr2 # => ["A", "B", "C"] | |
p arr1.object_id | |
p arr2.object_id | |
p arr1.object_id == arr2.object_id # false | |
p arr1[0].object_id | |
p arr2[0].object_id | |
p arr1[0].object_id == arr2[0].object_id # true |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment