0
0
Rubyprogramming~10 mins

Object#dup and Object#clone in Ruby - Interactive Code Practice

Choose your learning style9 modes available
Practice - 5 Tasks
Answer the questions below
1fill in blank
easy

Complete the code to create a shallow copy of the object using the correct method.

Ruby
copy = original.[1]
Drag options to blanks, or click blank then click option'
Adup
Bclone
Ccopy
Dnew
Attempts:
3 left
💡 Hint
Common Mistakes
Using clone instead of dup when singleton methods are not needed.
Trying to use a non-existent method like copy or new.
2fill in blank
medium

Complete the code to create a copy of the object including its singleton methods.

Ruby
copy = original.[1]
Drag options to blanks, or click blank then click option'
Acopy
Bclone
Cdup
Dnew
Attempts:
3 left
💡 Hint
Common Mistakes
Using dup when singleton methods need to be copied.
Using non-existent methods like copy or new.
3fill in blank
hard

Fix the error in the code to correctly duplicate the object without copying singleton methods.

Ruby
new_obj = obj.[1]
Drag options to blanks, or click blank then click option'
Acopy
Bclone
Cdup
Dnew
Attempts:
3 left
💡 Hint
Common Mistakes
Using clone which copies singleton methods.
Using non-existent methods like copy or new.
4fill in blank
hard

Fill both blanks to create a clone of the object and check if the clone is frozen.

Ruby
cloned_obj = obj.[1]
is_frozen = cloned_obj.[2]
Drag options to blanks, or click blank then click option'
Aclone
Bdup
Cfrozen?
Dfreeze
Attempts:
3 left
💡 Hint
Common Mistakes
Using dup instead of clone when singleton methods are needed.
Using freeze instead of frozen? which is a predicate method.
5fill in blank
hard

Fill all three blanks to duplicate an object, freeze the duplicate, and check if it is frozen.

Ruby
duplicate = obj.[1]
duplicate.[2]
is_frozen = duplicate.[3]
Drag options to blanks, or click blank then click option'
Adup
Bfreeze
Cfrozen?
Dclone
Attempts:
3 left
💡 Hint
Common Mistakes
Using clone instead of dup when singleton methods are not needed.
Using frozen? instead of freeze to make the object immutable.
Using freeze instead of frozen? to check frozen state.