Project

General

Profile

Feature #12092 ยป 0001-Allow-clone-to-yield-cloned-object-before-freezing.patch

jeremyevans0 (Jeremy Evans), 02/19/2016 08:33 PM

View differences:

object.c
316 316
 *     s1.inspect          #=> "#<Klass:0x401b3a38 @str=\"Hi\">"
317 317
 *     s2.inspect          #=> "#<Klass:0x401b3998 @str=\"Hi\">"
318 318
 *
319
 *  If a block is given, it is yielded the cloned object before the
320
 *  cloned object is frozen (if the receiver is frozen).
321
 *
319 322
 *  This method may have class-specific behavior.  If so, that
320 323
 *  behavior will be documented under the #+initialize_copy+ method of
321 324
 *  the class.
......
342 345

  
343 346
    init_copy(clone, obj);
344 347
    rb_funcall(clone, id_init_clone, 1, obj);
348
    if (rb_block_given_p()) {
349
	rb_yield(clone);
350
    }
345 351
    RBASIC(clone)->flags |= RBASIC(obj)->flags & FL_FREEZE;
346 352

  
347 353
    return clone;
test/ruby/test_object.rb
39 39
    assert_throw(:initialize_dup) {obj.dup}
40 40
  end
41 41

  
42
  def test_clone_with_block
43
    cls = Class.new do
44
      attr_accessor :b
45
    end
46
    obj = cls.new
47
    obj.b = 1
48
    obj.freeze
49
    c = obj.clone{|clone| clone.b = 2}
50
    assert_equal 2, c.b
51
    assert c.frozen?
52
  end
53

  
42 54
  def test_instance_of
43 55
    assert_raise(TypeError) { 1.instance_of?(1) }
44 56
  end