Bug #6037

#freeze bug

Added by Denis de Bernardy about 2 years ago. Updated over 1 year ago.

[ruby-core:42687]
Status:Rejected
Priority:Normal
Assignee:-
Category:-
Target version:-
ruby -v:1.9.3 Backport:

Description

freeze should deep freeze, to avoid inconsistent behavior:

array = ['a', 'b'].freeze
array[0] = 'b'
array[0].gsub!(/.+/s, 'b')
array

RUBY_VERSION
=> "1.9.3"
array = ['a', 'b'].freeze
=> ["a", "b"]
array[0] = 'b'
RuntimeError: can't modify frozen Array
from (irb):269:in []='
from (irb):269
from /opt/local/bin/irb:12:in
'
array[0].gsub!(/.+/s, 'b')
=> "b"
array
=> ["b", "b"]

History

#1 Updated by Marc-Andre Lafortune about 2 years ago

  • Status changed from Open to Rejected

Rejecting, as this is per spec. Behavior can not be changed without breaking a lot of code.

Note that I also would like a deep freeze (see http://bugs.ruby-lang.org/issues/show/2509 ) but that is a feature request, not a bug report.

#2 Updated by Denis de Bernardy over 1 year ago

Adding a note in case someone new to ruby ever wonders why it got rejected.

>> array = ['a', 'b'].freeze
=> ["a", "b"]
>> puts array.map { |o| o.object_id }
70282007609720
70282007609700
=> nil
>> array[0].gsub!(/.+/s, 'b')
=> "b"
>> puts array.map { |o| o.object_id }
70282007609720
70282007609700
=> nil

Also available in: Atom PDF