Skip to content

Instantly share code, notes, and snippets.

@EmilReji
Created February 25, 2019 00:57
Show Gist options
  • Save EmilReji/15fbf7002b46882b50064036288e6d90 to your computer and use it in GitHub Desktop.
Save EmilReji/15fbf7002b46882b50064036288e6d90 to your computer and use it in GitHub Desktop.
Nested Data Structures Shallow Copy Question
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
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