@@ -299,11 +299,12 @@ init_copy(VALUE dest, VALUE obj)
299
299
300
300
/*
301
301
* call-seq:
302
- * obj.clone -> an_object
302
+ * obj.clone(freeze: true) -> 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.
306
- * <code>clone</code> copies the frozen and tainted state of <i>obj</i>.
306
+ * <code>clone</code> copies the frozen (unless :freeze keyword argument
307
+ * is given with a false value) and tainted state of <i>obj</i>.
307
308
* See also the discussion under <code>Object#dup</code>.
308
309
*
309
310
* class Klass
@@ -321,11 +322,25 @@ init_copy(VALUE dest, VALUE obj)
321
322
* the class.
322
323
*/
323
324
324
- VALUE
325
- rb_obj_clone ( VALUE obj )
325
+ static VALUE
326
+ rb_obj_clone2 ( int argc , VALUE * argv , VALUE obj )
326
327
{
328
+ static ID keyword_ids [1 ];
329
+ VALUE opt ;
330
+ VALUE kwargs [1 ];
327
331
VALUE clone ;
328
332
VALUE singleton ;
333
+ VALUE kwfreeze = Qtrue ;
334
+ int n ;
335
+
336
+ if (!keyword_ids [0 ]) {
337
+ CONST_ID (keyword_ids [0 ], "freeze" );
338
+ }
339
+ n = rb_scan_args (argc , argv , "0:" , & opt );
340
+ if (!NIL_P (opt )) {
341
+ rb_get_kwargs (opt , keyword_ids , 0 , 1 , kwargs );
342
+ kwfreeze = kwargs [0 ];
343
+ }
329
344
330
345
if (rb_special_const_p (obj )) {
331
346
rb_raise (rb_eTypeError , "can't clone %s" , rb_obj_classname (obj ));
@@ -342,11 +357,20 @@ rb_obj_clone(VALUE obj)
342
357
343
358
init_copy (clone , obj );
344
359
rb_funcall (clone , id_init_clone , 1 , obj );
345
- RBASIC (clone )-> flags |= RBASIC (obj )-> flags & FL_FREEZE ;
360
+
361
+ if (Qfalse != kwfreeze ) {
362
+ RBASIC (clone )-> flags |= RBASIC (obj )-> flags & FL_FREEZE ;
363
+ }
346
364
347
365
return clone ;
348
366
}
349
367
368
+ VALUE
369
+ rb_obj_clone (VALUE obj )
370
+ {
371
+ return rb_obj_clone2 (0 , NULL , obj );
372
+ }
373
+
350
374
/*
351
375
* call-seq:
352
376
* obj.dup -> an_object
@@ -3424,7 +3448,7 @@ InitVM_Object(void)
3424
3448
3425
3449
rb_define_method (rb_mKernel , "class" , rb_obj_class , 0 );
3426
3450
rb_define_method (rb_mKernel , "singleton_class" , rb_obj_singleton_class , 0 );
3427
- rb_define_method (rb_mKernel , "clone" , rb_obj_clone , 0 );
3451
+ rb_define_method (rb_mKernel , "clone" , rb_obj_clone2 , -1 );
3428
3452
rb_define_method (rb_mKernel , "dup" , rb_obj_dup , 0 );
3429
3453
rb_define_method (rb_mKernel , "itself" , rb_obj_itself , 0 );
3430
3454
rb_define_method (rb_mKernel , "initialize_copy" , rb_obj_init_copy , 1 );
0 commit comments