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
299 299

  
300 300
/*
301 301
 *  call-seq:
302
 *     obj.clone -> an_object
302
 *     obj.clone(other=nil) -> an_object
303 303
 *
304 304
 *  Produces a shallow copy of <i>obj</i>---the instance variables of
305 305
 *  <i>obj</i> are copied, but not the objects they reference.
......
316 316
 *     s1.inspect          #=> "#<Klass:0x401b3a38 @str=\"Hi\">"
317 317
 *     s2.inspect          #=> "#<Klass:0x401b3998 @str=\"Hi\">"
318 318
 *
319
 *  You can provide another object to this method, which +initialize_clone+
320
 *  will be called with in addition to the object's clone, before the object's
321
 *  object is frozen.
322
 *
319 323
 *  This method may have class-specific behavior.  If so, that
320 324
 *  behavior will be documented under the #+initialize_copy+ method of
321 325
 *  the class.
322 326
 */
323 327

  
324
VALUE
325
rb_obj_clone(VALUE obj)
328
static VALUE
329
rb_obj_clone2(int argc, VALUE *argv, VALUE obj)
326 330
{
327 331
    VALUE clone;
328 332
    VALUE singleton;
......
341 345
    }
342 346

  
343 347
    init_copy(clone, obj);
344
    rb_funcall(clone, id_init_clone, 1, obj);
348
    rb_check_arity(argc, 0, 1);
349
    if (argc == 0) {
350
      rb_funcall(clone, id_init_clone, 1, obj);
351
    } else {
352
      rb_funcall(clone, id_init_clone, 2, obj, argv[0]);
353
    }
345 354
    RBASIC(clone)->flags |= RBASIC(obj)->flags & FL_FREEZE;
346 355

  
347 356
    return clone;
348 357
}
349 358

  
359
VALUE
360
rb_obj_clone(VALUE obj)
361
{
362
    return rb_obj_clone2(0, NULL, obj);
363
}
364

  
350 365
/*
351 366
 *  call-seq:
352 367
 *     obj.dup -> an_object
......
3424 3439

  
3425 3440
    rb_define_method(rb_mKernel, "class", rb_obj_class, 0);
3426 3441
    rb_define_method(rb_mKernel, "singleton_class", rb_obj_singleton_class, 0);
3427
    rb_define_method(rb_mKernel, "clone", rb_obj_clone, 0);
3442
    rb_define_method(rb_mKernel, "clone", rb_obj_clone2, -1);
3428 3443
    rb_define_method(rb_mKernel, "dup", rb_obj_dup, 0);
3429 3444
    rb_define_method(rb_mKernel, "itself", rb_obj_itself, 0);
3430 3445
    rb_define_method(rb_mKernel, "initialize_copy", rb_obj_init_copy, 1);