Project

General

Profile

Feature #12092 » 0001-Allow-clone-to-take-a-second-argument-passed-to-init.patch

Alternative approach #2 - jeremyevans0 (Jeremy Evans), 03/14/2016 06:05 PM

View differences:

object.c
/*
* call-seq:
* obj.clone -> an_object
* obj.clone(other=nil) -> 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.
......
* s1.inspect #=> "#<Klass:0x401b3a38 @str=\"Hi\">"
* s2.inspect #=> "#<Klass:0x401b3998 @str=\"Hi\">"
*
* You can provide another object to this method, which +initialize_clone+
* will be called with in addition to the object's clone, before the object's
* object is frozen.
*
* 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_clone(VALUE obj)
static VALUE
rb_obj_clone2(int argc, VALUE *argv, VALUE obj)
{
VALUE clone;
VALUE singleton;
......
}
init_copy(clone, obj);
rb_funcall(clone, id_init_clone, 1, obj);
rb_check_arity(argc, 0, 1);
if (argc == 0) {
rb_funcall(clone, id_init_clone, 1, obj);
} else {
rb_funcall(clone, id_init_clone, 2, obj, argv[0]);
}
RBASIC(clone)->flags |= RBASIC(obj)->flags & FL_FREEZE;
return clone;
}
VALUE
rb_obj_clone(VALUE obj)
{
return rb_obj_clone2(0, NULL, obj);
}
/*
* call-seq:
* obj.dup -> an_object
......
rb_define_method(rb_mKernel, "class", rb_obj_class, 0);
rb_define_method(rb_mKernel, "singleton_class", rb_obj_singleton_class, 0);
rb_define_method(rb_mKernel, "clone", rb_obj_clone, 0);
rb_define_method(rb_mKernel, "clone", rb_obj_clone2, -1);
rb_define_method(rb_mKernel, "dup", rb_obj_dup, 0);
rb_define_method(rb_mKernel, "itself", rb_obj_itself, 0);
rb_define_method(rb_mKernel, "initialize_copy", rb_obj_init_copy, 1);
(2-2/2)