Project

General

Profile

Actions

Feature #12300

closed

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

Added by jeremyevans0 (Jeremy Evans) over 8 years ago. Updated over 8 years ago.

Status:
Closed
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.


Files

Actions

Also available in: Atom PDF

Like0
Like0Like0Like0