Project

General

Profile

Bug #14266

Set#clone(freeze: false) makes frozen internal hash

Added by znz (Kazuhiro NISHIYAMA) 10 months ago. Updated 9 months ago.

Status:
Open
Priority:
Normal
Target version:
-
ruby -v:
ruby 2.6.0dev (2018-01-01 trunk 61537) [x86_64-darwin16]
[ruby-core:84581]

Description

% irb -r irb/completion --simple-prompt
>> require 'set'
=> true
>> set=Set[].freeze.clone(freeze: false)
=> #<Set: {}>
>> set.frozen?
=> false
>> set.instance_variable_get(:@hash).frozen?
=> true

In Set#initialize_clone, clone hash without freeze keyword argument.
But I think there is no easy way how to know freeze keyword argument value in initialize_clone.

  # Clone internal hash.
  def initialize_clone(orig)
    super
    @hash = orig.instance_variable_get(:@hash).clone
  end

History

#1 [ruby-core:84625] Updated by jeremyevans0 (Jeremy Evans) 10 months ago

I see two possible ways to fix this:

1) Switch to overriding clone instead of initialize_clone in such cases.

2) Make clone(freeze: false) call initialize_clone(freeze: false), but have clone otherwise call initialize_clone without a keyword argument. Make Object#initialize_clone accept and ignore the freeze keyword. This way, if you override initialize_clone and don't have it accept the freeze keyword, clone(freeze: false) will raise an ArgumentError. That's probably better than returning a unfrozen object with frozen instance variables.

#2 [ruby-core:84671] Updated by znz (Kazuhiro NISHIYAMA) 9 months ago

  • Assignee set to knu (Akinori MUSHA)

Also available in: Atom PDF