Forum: Ruby-dev [Closed] Module#initialize_copy does not clean the tables

F1d6cc2b735bfd82c8773172da2aeab9?d=identicon&s=25 Nobuyoshi Nakada (nobu)
on 2014-05-08 07:35
(Received via mailing list)
Issue #9813 has been updated by Nobuyoshi Nakada.

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 Nakada
* Status: Closed
* Priority: Normal
* Assignee:
* Category: core
* Target version:
* ruby -v: r45873
* Backport: 2.0.0: REQUIRED, 2.1: REQUIRED
----------------------------------------
`Module#initialize_copy`で定数やインスタンス変数を持たない`Module`をコピーしても、元の定数やインスタンス変数が残っています。

~~~ruby
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 topic is locked and can not be replied to.