Project

General

Profile

Feature #12300

Allow Object#clone to take freeze: false keyword argument to not freeze the clone

Added by jeremyevans0 (Jeremy Evans) about 1 year ago. Updated 11 months ago.

Status:
Closed
Priority:
Normal
Assignee:
-
Target version:
-
[ruby-core:75017]

Description

This allows Object#clone to not freeze the resulting object, which fixes a long standing problem in ruby in that you cannot get an unfrozen copy of an object that includes a copy of the object's singleton class. Previously, clone always returned a frozen object, and dup did not copy the singleton class. This adds the ability to get an unfrozen clone of an object that includes a copy of the object's singleton class, allowing further modification.

Among other things, this allows the ability to create modified clones of objects that have singleton classes:

class A
  def clone(arg)
    obj = super(freeze: false)
    # modify obj based on arg
    obj.freeze
  end
end

I think this is a simpler and more flexible approach to the same problem that #12092 tries to solve.

0001-Allow-clone-to-take-freeze-false-keyword-argument-to.patch View (3.93 KB) jeremyevans0 (Jeremy Evans), 04/19/2016 12:38 AM

Associated revisions

Revision 55786
Added by shyouhei (Shyouhei Urabe) 11 months ago

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.
  [Feature #12300]

* test/ruby/test_object.rb (TestObject): test for it.

Revision 55786
Added by shyouhei (Shyouhei Urabe) 11 months ago

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.
  [Feature #12300]

* test/ruby/test_object.rb (TestObject): test for it.

Revision 55808
Added by nobu (Nobuyoshi Nakada) 11 months ago

object.c: restrict freeze option

  • object.c (rb_obj_clone2): restrict freeze option to true other than false which only has the effect. [Feature #12300]

Revision 55808
Added by nobu (Nobuyoshi Nakada) 11 months ago

object.c: restrict freeze option

  • object.c (rb_obj_clone2): restrict freeze option to true other than false which only has the effect. [Feature #12300]

Revision 55809
Added by nobu (Nobuyoshi Nakada) 11 months ago

NEWS: Kernel#clone [ci skip]

  • NEWS (Kernel#clone): mention freeze flag. [Feature #12300]

Revision 55809
Added by nobu (Nobuyoshi Nakada) 11 months ago

NEWS: Kernel#clone [ci skip]

  • NEWS (Kernel#clone): mention freeze flag. [Feature #12300]

History

#1 [ruby-core:75033] Updated by shyouhei (Shyouhei Urabe) about 1 year ago

Sounds much simpler than the previous one to understand what is happening. Also it seems backwards-compatible.

Given the needs of this kind, I'd like to +1.

#2 [ruby-core:76409] Updated by matz (Yukihiro Matsumoto) 11 months ago

Sounds reasonable.

Matz.

#3 Updated by shyouhei (Shyouhei Urabe) 11 months ago

  • Status changed from Open to Closed

Applied in changeset r55786.


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.
  [Feature #12300]

* test/ruby/test_object.rb (TestObject): test for it.

Also available in: Atom PDF