以下の内容はhttps://unageanu.hatenablog.com/entry/20080810/1218357723より取得しました。


モジュールからクラス定義を消す

remove_constでモジュールからクラス定義を消すことができます。

module Test
  class Foo; end
end

# クラス定義を消す。
# remove_constはprivateメソッドなのでsendで送る。
Test.send(:remove_const, :Foo)

# 消されたクラスは利用できなくなる。
begin
  Test::Foo
  raise "あれ?"
rescue NameError
end
begin
  Test::Foo.new # インスタンス化も当然エラー
  raise "あれ?"
rescue NameError
end

でも、ObjectSpaceから探すと見つかったり。蓋がされてるだけっぽいなー。

# ObjectSpaceから探すと見つかる
ObjectSpace.each_object(Class) {|cl|
  next unless cl.name == "Test::Foo"
  p cl
  p cl.new 
}

実行結果です。

Test::Foo
#<Test::Foo:0x10030bc8>



以上の内容はhttps://unageanu.hatenablog.com/entry/20080810/1218357723より取得しました。
このページはhttp://font.textar.tv/のウェブフォントを使用してます

不具合報告/要望等はこちらへお願いします。
モバイルやる夫Viewer Ver0.14