Bird
0
0

What is the main difference between dup and clone in Ruby?

easy📝 Conceptual Q11 of 15
Ruby - Class Methods and Variables
What is the main difference between dup and clone in Ruby?
A<code>clone</code> copies frozen state and singleton methods; <code>dup</code> does not.
B<code>dup</code> copies frozen state and singleton methods; <code>clone</code> does not.
C<code>dup</code> creates a deep copy; <code>clone</code> creates a shallow copy.
D<code>clone</code> only copies instance variables; <code>dup</code> copies the entire object.
Step-by-Step Solution
Solution:
  1. Step 1: Understand what dup copies

    dup creates a shallow copy but does not copy frozen state or singleton methods.
  2. Step 2: Understand what clone copies

    clone copies everything including frozen state and singleton methods.
  3. Final Answer:

    clone copies frozen state and singleton methods; dup does not. -> Option A
  4. Quick Check:

    clone copies all [OK]
Quick Trick: Remember: clone copies everything, dup skips frozen and singleton [OK]
Common Mistakes:
  • Thinking dup copies frozen state
  • Confusing shallow and deep copy
  • Assuming clone skips singleton methods

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Ruby Quizzes