Skip to content

Commit 320ae01

Browse files
committed
Object#clone with freeze: false [Feature #12300]
* object.c (rb_obj_clone2): Allow Object#clone to take freeze: false keyword argument to not freeze the clone. [ruby-core:75017][Feature #12300] * test/ruby/test_object.rb (TestObject): test for it. git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@55786 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
1 parent ac44784 commit 320ae01

File tree

3 files changed

+54
-6
lines changed

3 files changed

+54
-6
lines changed

ChangeLog

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,11 @@
1+
Mon Aug 1 14:50:06 2016 Jeremy Evans <code@jeremyevans.net>
2+
3+
* object.c (rb_obj_clone2): Allow Object#clone to take freeze:
4+
false keyword argument to not freeze the clone.
5+
[ruby-core:75017][Feature #12300]
6+
7+
* test/ruby/test_object.rb (TestObject): test for it.
8+
19
Mon Aug 1 12:16:19 2016 SHIBATA Hiroshi <hsbt@ruby-lang.org>
210

311
* ext/json/*, test/json/json_parser_test.rb: Update json-2.0.2.

object.c

Lines changed: 30 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -299,11 +299,12 @@ init_copy(VALUE dest, VALUE obj)
299299

300300
/*
301301
* call-seq:
302-
* obj.clone -> an_object
302+
* obj.clone(freeze: true) -> an_object
303303
*
304304
* Produces a shallow copy of <i>obj</i>---the instance variables of
305305
* <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>.
307308
* See also the discussion under <code>Object#dup</code>.
308309
*
309310
* class Klass
@@ -321,11 +322,25 @@ init_copy(VALUE dest, VALUE obj)
321322
* the class.
322323
*/
323324

324-
VALUE
325-
rb_obj_clone(VALUE obj)
325+
static VALUE
326+
rb_obj_clone2(int argc, VALUE *argv, VALUE obj)
326327
{
328+
static ID keyword_ids[1];
329+
VALUE opt;
330+
VALUE kwargs[1];
327331
VALUE clone;
328332
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+
}
329344

330345
if (rb_special_const_p(obj)) {
331346
rb_raise(rb_eTypeError, "can't clone %s", rb_obj_classname(obj));
@@ -342,11 +357,20 @@ rb_obj_clone(VALUE obj)
342357

343358
init_copy(clone, obj);
344359
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+
}
346364

347365
return clone;
348366
}
349367

368+
VALUE
369+
rb_obj_clone(VALUE obj)
370+
{
371+
return rb_obj_clone2(0, NULL, obj);
372+
}
373+
350374
/*
351375
* call-seq:
352376
* obj.dup -> an_object
@@ -3424,7 +3448,7 @@ InitVM_Object(void)
34243448

34253449
rb_define_method(rb_mKernel, "class", rb_obj_class, 0);
34263450
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);
34283452
rb_define_method(rb_mKernel, "dup", rb_obj_dup, 0);
34293453
rb_define_method(rb_mKernel, "itself", rb_obj_itself, 0);
34303454
rb_define_method(rb_mKernel, "initialize_copy", rb_obj_init_copy, 1);

test/ruby/test_object.rb

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,22 @@ def test_dup
2828
end
2929
end
3030

31+
def test_clone
32+
a = Object.new
33+
def a.b; 2 end
34+
35+
a.freeze
36+
c = a.clone
37+
assert_equal(true, c.frozen?)
38+
assert_equal(2, c.b)
39+
40+
d = a.clone(freeze: false)
41+
def d.e; 3; end
42+
assert_equal(false, d.frozen?)
43+
assert_equal(2, d.b)
44+
assert_equal(3, d.e)
45+
end
46+
3147
def test_init_dupclone
3248
cls = Class.new do
3349
def initialize_clone(orig); throw :initialize_clone; end

0 commit comments

Comments
 (0)