Skip to content

Set#dup does not seem to call #initialize_copy #6903

@postmodern

Description

@postmodern

I ran into this edge-case when working with classes which inherit from Set (I know, I probably shouldn't do that) and the classes contain other instance variables which require custom initialize_copy logic to copy them over when the set is duplicated, such as when Set#| is called; the implementation of Set#| on both CRuby and JRuby is essentially dup.merge(other).

Under JRuby (9.3.1.0), the custom initialize_copy method does not seem to be called when Set#dup is called.

Steps To Reproduce

require 'set'

class FooObject
  def initialize_copy(other)
    puts "#{self.class}#initialize_copy called!"
  end
end

class FooSet < Set
  def initialize_copy(other)
    puts "#{self.class}#initialize_copy called!"
  end
end

obj = FooObject.new
obj.dup

set = FooSet.new
set.dup

CRuby 2.7.4

FooObject#initialize_copy called!
FooSet#initialize_copy called!

CRuby 3.0.2

FooObject#initialize_copy called!
FooSet#initialize_copy called!

JRuby 9.3.1.0

FooObject#initialize_copy called!

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions