Ruby中,如何实现类加载时的回调机制?
- 内容介绍
- 文章标签
- 相关推荐
本文共计202个文字,预计阅读时间需要1分钟。
是否有可以在加载类时执行的回调?我想实现这样的功能。使用 `register_callback('Foo', :debug_message)`,然后在 `debug_message` 中输出 Foo has been loaded。但 `require 'foo'` 不存在,因为 Ruby 中的类是开放的。
是否有一个可以在加载类时执行的回调.我在想这样的事情.register_callback('Foo', :debug_message) def debug_message puts "Foo has been loaded" end require 'foo' 不,那里没有.并且不可能,因为Ruby中的类是开放的简单原因:它们永远不会完全“加载”,您可以随时添加,删除,重命名和覆盖方法.
例如,以下类何时“加载”?
# 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
本文共计202个文字,预计阅读时间需要1分钟。
是否有可以在加载类时执行的回调?我想实现这样的功能。使用 `register_callback('Foo', :debug_message)`,然后在 `debug_message` 中输出 Foo has been loaded。但 `require 'foo'` 不存在,因为 Ruby 中的类是开放的。
是否有一个可以在加载类时执行的回调.我在想这样的事情.register_callback('Foo', :debug_message) def debug_message puts "Foo has been loaded" end require 'foo' 不,那里没有.并且不可能,因为Ruby中的类是开放的简单原因:它们永远不会完全“加载”,您可以随时添加,删除,重命名和覆盖方法.
例如,以下类何时“加载”?
# 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

