开发者

Callback when a class is loaded

开发者 https://www.devze.com 2023-03-31 08:57 出处:网络
Is there a callback which 开发者_C百科can be executed when aclass is loaded. I am thinking something like this.

Is there a callback which 开发者_C百科can be executed when a class is loaded. I am thinking something like this.

register_callback('Foo', :debug_message)

def debug_message
 puts "Foo has been loaded"
end

require 'foo'


No, there is not. And there cannot be, for the simple reason that classes in Ruby are open: they are never fully "loaded", you can always add, remove, rename and overwrite methods at any later point in time.

For example, when is the following class "loaded"?

# foo.rb
class Foo
  def some_method
  end
end

# bar.rb
class Foo
  def some_other_method
  end
end

# baz.rb
class Foo
  def some_method
  end
end

require 'foo'
require 'bar'

require 'baz' if rand > 0.5
0

精彩评论

暂无评论...
验证码 换一张
取 消