加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

ruby – 加载类时的回调

发布时间:2020-12-17 01:28:23 所属栏目:百科 来源:网络整理
导读:是否有一个可以在加载类时执行的回调.我在想这样的事情. register_callback('Foo',:debug_message)def debug_message puts "Foo has been loaded"endrequire '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

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读