0
0
Rubyprogramming~10 mins

Object#dup and Object#clone in Ruby - Step-by-Step Execution

Choose your learning style9 modes available
Concept Flow - Object#dup and Object#clone
Start with original object
Create shallow copy
#clone copies singleton methods
Return new object copy
The flow shows how calling #dup or #clone on an object creates a shallow copy, with #clone also copying singleton methods.
Execution Sample
Ruby
class MyClass
  def greet; 'hello'; end
end
obj = MyClass.new
copy = obj.dup
copy2 = obj.clone
This code creates an object and makes two copies using #dup and #clone.
Execution Table
StepActionObject StateCopy TypeSingleton Methods Copied
1Create objobj with greet methodN/AN/A
2Call obj.dupobj unchangedShallow copyNo
3Call obj.cloneobj unchangedShallow copyYes (if any)
4Modify copy instance varcopy changedcopy independentN/A
5Modify copy2 singleton methodcopy2 changedcopy independentYes
6EndOriginal and copies existN/AN/A
💡 Execution stops after creating copies and showing differences in singleton method copying.
Variable Tracker
VariableStartAfter dupAfter cloneFinal
objOriginal objectOriginal objectOriginal objectOriginal object
copyN/AShallow copy of objShallow copy of objModified copy instance var
copy2N/AN/AShallow copy of obj with singleton methodsModified copy2 singleton method
Key Moments - 2 Insights
Why does #clone copy singleton methods but #dup does not?
Because #clone is designed to copy the entire object including its singleton methods, while #dup only copies instance variables. See execution_table rows 2 and 3.
Are the copies completely independent from the original?
They are shallow copies, so instance variables are copied but nested objects inside them are shared. See variable_tracker for changes after dup and clone.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table at step 3, what is true about the copy created by #clone?
AIt creates a deep copy of nested objects
BIt copies singleton methods from the original object
CIt does not copy instance variables
DIt modifies the original object
💡 Hint
Check the 'Singleton Methods Copied' column at step 3 in execution_table.
At which step does modifying the copy's instance variable happen?
AStep 1
BStep 2
CStep 4
DStep 5
💡 Hint
Look at the 'Action' column describing modifications in execution_table.
If the original object has no singleton methods, what difference will #dup and #clone show?
ANo difference in copying singleton methods
B#clone will fail
C#dup will copy singleton methods anyway
DBoth will create deep copies
💡 Hint
Refer to the 'Singleton Methods Copied' column in execution_table steps 2 and 3.
Concept Snapshot
Object#dup and Object#clone create shallow copies.
#dup copies instance variables only.
#clone copies instance variables and singleton methods.
Neither performs deep copy of nested objects.
Use #clone to preserve singleton methods.
Full Transcript
This visual execution trace shows how Ruby's Object#dup and Object#clone work. Starting with an original object, calling #dup creates a shallow copy copying instance variables but not singleton methods. Calling #clone also copies singleton methods if any exist. The execution table traces each step: creating the original object, calling #dup and #clone, and modifying copies. The variable tracker shows how variables change after each operation. Key moments clarify common confusions about singleton methods and shallow copying. The quiz tests understanding of these differences. The snapshot summarizes the key points for quick reference.