Sometimes you want to make a copy of an object to change it without affecting the original. dup and clone help you do that.
Object#dup and Object#clone in Ruby
copy = original.dup copy = original.clone
dup makes a shallow copy without copying the object's frozen state or singleton methods.
clone makes a shallow copy including frozen state and singleton methods.
dup and clone create a new string with the same content.original = "hello" copy1 = original.dup copy2 = original.clone
dup copies the object but does not copy singleton methods if any.class MyClass def greet "hi" end end obj = MyClass.new copy = obj.dup
dup does not copy frozen state, but clone does.obj = "hello" obj.freeze copy1 = obj.dup copy2 = obj.clone puts copy1.frozen? puts copy2.frozen?
This program shows how dup and clone create copies of an object. Changing the copies does not change the original.
class Person attr_accessor :name end person1 = Person.new person1.name = "Alice" person2 = person1.dup person3 = person1.clone person2.name = "Bob" person3.name = "Carol" puts "person1 name: #{person1.name}" puts "person2 name: #{person2.name}" puts "person3 name: #{person3.name}"
Both dup and clone make shallow copies. If the object has other objects inside, those are not copied but shared.
clone copies the frozen state and singleton methods, dup does not.
Use dup when you want a simple copy. Use clone when you want an exact copy including special states.
dup and clone make copies of objects to avoid changing the original.
dup copies the object but skips frozen state and singleton methods.
clone copies everything including frozen state and singleton methods.
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
