Object#dup and Object#clone in Ruby - Time & Space Complexity
When we use dup or clone in Ruby, we create copies of objects. Understanding how long this copying takes helps us write faster programs.
We want to know: how does the time to copy grow as the object gets bigger or more complex?
Analyze the time complexity of the following Ruby code snippet.
obj = [1, 2, 3, 4, 5]
copy1 = obj.dup
copy2 = obj.clone
This code creates two copies of an array object using dup and clone.
Identify the loops, recursion, array traversals that repeat.
- Primary operation: copying each element inside the object (array) one by one
- How many times: once for each element in the array
As the object gets bigger, copying takes longer because each element must be copied.
| Input Size (n) | Approx. Operations |
|---|---|
| 10 | About 10 element references copied |
| 100 | About 100 element references copied |
| 1000 | About 1000 element references copied |
Pattern observation: The time grows directly with the number of elements. Double the elements, double the work.
Time Complexity: O(n)
This means copying takes time proportional to the size of the object being copied.
[X] Wrong: "Copying an object is always instant and does not depend on its size."
[OK] Correct: Copying must handle each part of the object, so bigger objects take more time.
Knowing how copying objects scales helps you explain performance in real programs. It shows you understand what happens behind the scenes when you duplicate data.
"What if the object contains other objects that also need to be copied? How would that affect the time complexity?"
Practice
dup and clone in Ruby?Solution
Step 1: Understand what
dupcopiesdupcreates a shallow copy but does not copy frozen state or singleton methods.Step 2: Understand what
clonecopiesclonecopies everything including frozen state and singleton methods.Final Answer:
clonecopies frozen state and singleton methods;dupdoes not. -> Option AQuick Check:
clone copies all [OK]
- Thinking dup copies frozen state
- Confusing shallow and deep copy
- Assuming clone skips singleton methods
obj using dup?Solution
Step 1: Identify the method call syntax
In Ruby,dupis called on the object asobj.duporobj.dup().Step 2: Check other options
cloneis a different method,dup(obj)andcopy()are invalid.Final Answer:
copy = obj.dup() -> Option AQuick Check:
Use obj.dup() to copy [OK]
- Using clone instead of dup
- Trying dup(obj) as a function
- Using non-existent copy() method
class MyClass
def greet
"Hello"
end
end
obj = MyClass.new
obj_singleton = obj.clone
def obj_singleton.greet
"Hi"
end
copy1 = obj.dup
copy2 = obj.clone
puts copy1.greet
puts copy2.greetSolution
Step 1: Understand singleton method copying
Singleton methods are copied byclonebut not bydup. Here,obj_singletonhas a singleton methodgreetreturning "Hi".Step 2: Check what copy1 and copy2 get
copy1 = obj.dupdoes not copy singleton methods, socopy1.greetcalls original method returning "Hello".copy2 = obj.clonecopies singleton methods only fromobj, not fromobj_singleton. Sinceobjhas no singleton method,copy2.greetreturns "Hello".Final Answer:
Hello Hello -> Option DQuick Check:
dup and clone copy singleton only from original object [OK]
- Assuming clone copies singleton methods from clones
- Thinking dup copies singleton methods
- Expecting 'Hi' output from copy2
str = "hello" str.freeze copy = str.dup puts copy.frozen?What is the output and why? If there is an error, identify and fix it.
Solution
Step 1: Understand freezing and dup behavior
The stringstris frozen. Whendupis called, it copies the object but does NOT copy the frozen state.Step 2: Check frozen state of copy
Sincedupskips frozen state,copy.frozen?returnsfalse.Final Answer:
false -> Option BQuick Check:
dup skips frozen state, so frozen? = false [OK]
- Expecting copy to be frozen
- Confusing frozen? method with freeze
- Assuming dup raises error on frozen objects
obj with singleton methods and it is frozen. You want to create a copy that preserves both the singleton methods and the frozen state. Which method should you use and why?Solution
Step 1: Identify method that copies singleton methods and frozen state
dupskips frozen state and singleton methods, so it is not suitable.Step 2: Confirm
clonebehaviorclonecopies both singleton methods and frozen state automatically.Final Answer:
Useobj.clonebecause it copies both singleton methods and frozen state. -> Option CQuick Check:
clone copies all including frozen and singleton [OK]
- Using dup and expecting frozen state copied
- Manually adding singleton methods after clone
- Freezing after dup instead of cloning
Ready to prove it?
Pass the test and this pattern moves unknown
