Project

General

Profile

Actions

Feature #12092

closed

Allow Object#clone to yield cloned object before freezing

Added by jeremyevans0 (Jeremy Evans) almost 9 years ago. Updated about 8 years ago.

Status:
Rejected
Assignee:
-
Target version:
-
[ruby-core:73898]

Description

This allows creating modified clones of frozen objects that have
singleton classes:

a = [1,2,3]
def a.fl; first + last; end
a.freeze
a.fl # => 4

clone = a.clone{|c| c << 10}
clone.last # => 10
clone.fl # => 11
clone.frozen? # => true

Previously, this was not possible at all. If an object was
frozen, the clone was frozen before the cloned object could
be modified. It was possible to modify the clone using
initialize_clone or initialize_copy, but you couldn't change how
to modify the clone on a per-call basis. You couldn't use dup
to return an unfrozen copy, modify it, and then freeze it, because
dup doesn't copy singleton classes.

This allows ruby to be used in a functional style with immutable
data structures, while still keeping the advantages of singleton
classes.


Files


Related issues 1 (0 open1 closed)

Related to Ruby master - Feature #14778: Make Object#dup accept a blockFeedbackActions
Actions

Also available in: Atom PDF

Like0
Like0Like0Like0Like0Like0Like0Like0Like0Like0