summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--ChangeLog8
-rw-r--r--object.c36
-rw-r--r--test/ruby/test_object.rb16
3 files changed, 54 insertions, 6 deletions
diff --git a/ChangeLog b/ChangeLog
index 8af164a8d7..2071f1a0a6 100644
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,3 +1,11 @@
+Mon Aug 1 14:50:06 2016 Jeremy Evans <code@jeremyevans.net>
+
+ * 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.
+
Mon Aug 1 12:16:19 2016 SHIBATA Hiroshi <hsbt@ruby-lang.org>
* ext/json/*, test/json/json_parser_test.rb: Update json-2.0.2.
diff --git a/object.c b/object.c
index 41cdbb69d2..5cd32630e4 100644
--- a/object.c
+++ b/object.c
@@ -299,11 +299,12 @@ init_copy(VALUE dest, VALUE obj)
/*
* call-seq:
- * obj.clone -> an_object
+ * obj.clone(freeze: true) -> 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>clone</code> copies the frozen and tainted state of <i>obj</i>.
+ * <code>clone</code> copies the frozen (unless :freeze keyword argument
+ * is given with a false value) and tainted state of <i>obj</i>.
* See also the discussion under <code>Object#dup</code>.
*
* class Klass
@@ -321,11 +322,25 @@ init_copy(VALUE dest, VALUE obj)
* the class.
*/
-VALUE
-rb_obj_clone(VALUE obj)
+static VALUE
+rb_obj_clone2(int argc, VALUE *argv, VALUE obj)
{
+ static ID keyword_ids[1];
+ VALUE opt;
+ VALUE kwargs[1];
VALUE clone;
VALUE singleton;
+ VALUE kwfreeze = Qtrue;
+ int n;
+
+ if (!keyword_ids[0]) {
+ CONST_ID(keyword_ids[0], "freeze");
+ }
+ n = rb_scan_args(argc, argv, "0:", &opt);
+ if (!NIL_P(opt)) {
+ rb_get_kwargs(opt, keyword_ids, 0, 1, kwargs);
+ kwfreeze = kwargs[0];
+ }
if (rb_special_const_p(obj)) {
rb_raise(rb_eTypeError, "can't clone %s", rb_obj_classname(obj));
@@ -342,11 +357,20 @@ rb_obj_clone(VALUE obj)
init_copy(clone, obj);
rb_funcall(clone, id_init_clone, 1, obj);
- RBASIC(clone)->flags |= RBASIC(obj)->flags & FL_FREEZE;
+
+ if (Qfalse != kwfreeze) {
+ 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
@@ -3424,7 +3448,7 @@ InitVM_Object(void)
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);
diff --git a/test/ruby/test_object.rb b/test/ruby/test_object.rb
index 62390ad5b5..599b2ae140 100644
--- a/test/ruby/test_object.rb
+++ b/test/ruby/test_object.rb
@@ -28,6 +28,22 @@ class TestObject < Test::Unit::TestCase
end
end
+ def test_clone
+ a = Object.new
+ def a.b; 2 end
+
+ a.freeze
+ c = a.clone
+ assert_equal(true, c.frozen?)
+ assert_equal(2, c.b)
+
+ d = a.clone(freeze: false)
+ def d.e; 3; end
+ assert_equal(false, d.frozen?)
+ assert_equal(2, d.b)
+ assert_equal(3, d.e)
+ end
+
def test_init_dupclone
cls = Class.new do
def initialize_clone(orig); throw :initialize_clone; end