Skip to content

Commit

Permalink
fix: formatting
Browse files Browse the repository at this point in the history
  • Loading branch information
2KAbhishek committed Nov 9, 2023
1 parent 98756e0 commit ae7f27c
Showing 1 changed file with 20 additions and 6 deletions.
26 changes: 20 additions & 6 deletions content/blog/ruby-on-rails-dup-vs-deep-dup.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,11 @@ categories = ["Software Craftsmanship", "Ruby on Rails", "Dup", "Deep Dup"]
type = ""
+++

In Ruby on Rails, the `.dup` method is commonly used to create a duplicate of an object, allowing modifications without affecting the original. However, it's crucial to understand its limitations, especially when dealing with nested structures.
In Ruby on Rails, the `.dup` method is commonly used to create a duplicate of an object, allowing modifications without affecting the original.
However, it's crucial to understand its limitations, especially when dealing with nested structures.

## The Shallow Copy Pitfall

Consider the following scenario with a user object:

```ruby
Expand All @@ -30,7 +32,8 @@ puts user[:email]
# hello@gmail.com
```

Here, modifying the duplicated object (`user_dup`) doesn't impact the original object (`user`), and everything seems fine. However, things take an unexpected turn when dealing with nested attributes:
Here, modifying the duplicated object (`user_dup`) doesn't impact the original object (`user`), and everything seems fine.
However, things take an unexpected turn when dealing with nested attributes:

```ruby
user_dup[:address][:city] = 'Mumbai'
Expand All @@ -42,10 +45,15 @@ puts user[:address][:city]
# Mumbai
```

Surprisingly, changing the city for `user_dup` also alters the city for the original `user` object. The culprit here is the Shallow Copy nature of `.dup`. While it creates a new object with a different `object_id`, the nested attributes still reference the original object. This leads to unintended side effects when modifying nested values.
Surprisingly, changing the city for `user_dup` also alters the city for the original `user` object.
The culprit here is the Shallow Copy nature of `.dup`.
While it creates a new object with a different `object_id`, the nested attributes still reference the original object.
This leads to unintended side effects when modifying nested values.

## The Solution:
To avoid such pitfalls, it's recommended to use the `deep_dup` method instead of `dup` when dealing with objects containing deep nesting. Let's revisit the previous example with `deep_dup`:

To avoid such pitfalls, it's recommended to use the `deep_dup` method instead of `dup` when dealing with objects containing deep nesting.
Let's revisit the previous example with `deep_dup`:

```ruby
user = { name: 'Hello', email: 'hello@gmail.com', address: { city: 'Delhi' } }
Expand All @@ -61,7 +69,13 @@ puts user[:address][:city]
# Delhi
```

By using `deep_dup`, we ensure that the duplicated object is a deep copy, preventing unintended modifications to nested attributes. This practice is particularly crucial when dealing with complex data structures in Ruby on Rails applications.
By using `deep_dup`, we ensure that the duplicated object is a deep copy, preventing unintended modifications to nested attributes.
This practice is particularly crucial when dealing with complex data structures in Ruby on Rails applications.

## Conclusion
Understanding the distinction between `dup` and `deep_dup` is vital for avoiding unexpected behaviors, especially when working with nested objects in Ruby on Rails. While `dup` creates a shallow copy, retaining references to nested attributes, `deep_dup` ensures a complete duplication, safeguarding against unintended side effects. Choosing the appropriate method based on the depth of your data structure is key to maintaining the integrity of your objects.

Understanding the distinction between `dup` and `deep_dup` is vital for avoiding unexpected behaviors, especially when working with nested objects in Ruby on Rails.
While `dup` creates a shallow copy, retaining references to nested attributes, `deep_dup` ensures a complete duplication, safeguarding against unintended side effects.

Choosing the appropriate method based on the depth of your data structure is key to maintaining the integrity of your objects.

0 comments on commit ae7f27c

Please sign in to comment.