Shallow Copy

A shallow copy of an object is a copy of the collection structure, not its elements. In other word: if the object contains other objects, those won’t be copied and will be shared instead 1 .

In contrast, a deep copy copies everything.

Example in Ruby

Ruby only provides method to perform Shallow Copy. There is no built-in way to create a deep copy.

arr1 = ["flamingo", "capybara", "red panda"]
arr2 = arr1.dup
arr2[0].upcase!

arr2 # => ["FLAMINGO", "capybara", "red panda"]
arr2 # => ["FLAMINGO", "capybara", "red panda"]

In this example, both array are modified because the method String#upcase! was called on the object within the array (the string flamingo) rather than the array itself. As the String object is shared between the original arr1 and the copy arr2, and the method called is destructive, its effect can be seen on both collection.

Footnotes
Links to this page
#programming