Ruby and Rails have several methods for creating a new object that looks like another: clone
, dup
, deep_dup
. When using them you should be aware of their differences so that you can select the method you really need.
clone
frozen
, tainted
and modules that the object has been extended withdup
frozen
, tainted
and modules that the object has been extended withdeep_dup
frozen
, tainted
and singleton methods are ignored (like dup
)deep_dup
ed as welldeep_dup
eddup
, or self
when frozen. If your object has instance variables that are hashes or arrays, those will not be deep_dup
ed.Note that deep_dup
is not a silver bullet and needs to be implemented properly by the object you call it on.
Generally speaking, you should prefer deep_dup
over dup
, but need to know how the object itself implements it.