[Closed] Module#initialize_copy does not clean the tables

Issue #9813 has been updated by Nobuyoshi N…

Status changed from Open to Closed
% Done changed from 0 to 100

Applied in changeset r45874.


class.c: always clear tables first

  • class.c (rb_mod_init_copy): always clear instance variable,
    constant and method tables first, regardless the source tables.
    [ruby-dev:48182] [Bug #9813]

Bug #9813: Module#initialize_copy does not clean the tables
https://bugs.ruby-lang.org/issues/9813#change-46611

  • Author: Nobuyoshi N.
  • Status: Closed
  • Priority: Normal
  • Assignee:
  • Category: core
  • Target version:
  • ruby -v: r45873
  • Backport: 2.0.0: REQUIRED, 2.1: REQUIRED

Module#initialize_copyで定数やインスタンス変数を持たないModuleをコピーしても、元の定数やインスタンス変数が残っています。

m = Module.new do
  def x
  end
  const_set(:X, 1)
  @x = 2
end
p m.instance_methods, m.instance_variables, m.constants
#=> [:x]
#=> [:@x]
#=> [:X]
m.module_eval do
  initialize_copy(Module.new)
end
p m.instance_methods, m.instance_variables, m.constants
#=> []
#=> [:@x]
#=> [:X]

1.8でinitialize_copyが導入された当初からあるバグのようです。

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs