Produces a shallow copy of obj—the instance variables of
obj are copied, but not the objects they reference. dup copies the tainted state of
obj. See also the discussion under Object#clone. In general, clone and dup may have different semantics in
descendent classes. While clone
is used to duplicate an object, including its internal state, dup typically uses the class of the descendent object to create the
new instance.
This method may have class-specific
behavior. If so, that behavior will be documented under the #initialize_copymethod of the class.
/*
* call-seq:
* obj.dup -> an_object
*
* Produces a shallow copy of <i>obj</i>---the instance variables of
* <i>obj</i> are copied, but not the objects they reference.
* <code>dup</code> copies the tainted state of <i>obj</i>. See also
* the discussion under <code>Object#clone</code>. In general,
* <code>clone</code> and <code>dup</code> may have different semantics
* in descendent classes. While <code>clone</code> is used to duplicate
* an object, including its internal state, <code>dup</code> typically
* uses the class of the descendent object to create the new instance.
*
* This method may have class-specific behavior. If so, that
* behavior will be documented under the #+initialize_copy+ method of
* the class.
*/
VALUE
rb_obj_dup(obj)
VALUE obj;
{
VALUE dup;
if (rb_special_const_p(obj)) {
rb_raise(rb_eTypeError, "can't dup %s", rb_obj_classname(obj));
}
dup = rb_obj_alloc(rb_obj_class(obj));
init_copy(dup, obj);
return dup;
}
1Note
Dup vs Clone difference
aberezovskiy ยท Jul 15, 20111 thank
As for me main difference between .dup and .clone , that first one doesn't not freeze result object if initial object was frozen.