0
0
Rubyprogramming~5 mins

Object#dup and Object#clone in Ruby - Cheat Sheet & Quick Revision

Choose your learning style9 modes available
Recall & Review
beginner
What does Object#dup do in Ruby?

Object#dup creates a shallow copy of an object. It copies the object's instance variables but does not copy the object's singleton methods or frozen state.

Click to reveal answer
beginner
How is Object#clone different from Object#dup?

Object#clone also creates a shallow copy but copies the frozen state and singleton methods of the original object, unlike dup.

Click to reveal answer
intermediate
Does dup copy singleton methods of an object?

No, dup does not copy singleton methods. Only clone copies singleton methods.

Click to reveal answer
intermediate
What happens to the frozen state of an object when using dup and clone?

dup does not copy the frozen state; the new object is unfrozen. clone copies the frozen state, so if the original was frozen, the clone is frozen too.

Click to reveal answer
intermediate
Explain shallow copy in the context of dup and clone.

Both dup and clone create shallow copies, meaning they copy the object itself but not the objects referenced by its instance variables. Changes to nested objects affect both copies.

Click to reveal answer
Which method copies singleton methods of an object in Ruby?
Adup
Bcopy
Cnew
Dclone
If an object is frozen, which method preserves this frozen state in the copy?
Aclone
Bdup
Cboth dup and clone
Dneither dup nor clone
What kind of copy do dup and clone create?
AShallow copy
BDeep copy
CReference copy
DNo copy
Which of the following is NOT copied by dup?
ASingleton methods
BInstance variables
CObject's class
DObject's frozen state
After using dup on a frozen object, what is the frozen state of the new object?
AFrozen
BUnfrozen
CDepends on the object
DRaises an error
Describe the main differences between Object#dup and Object#clone in Ruby.
Think about what extra features clone copies compared to dup.
You got /4 concepts.
    Explain what shallow copy means when using dup or clone on an object.
    Consider what happens to objects inside the copied object.
    You got /3 concepts.