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

是否有类似于Class#的钩子,仅在Ruby类定义之后被触发?

发布时间:2020-12-16 20:45:00 所属栏目:百科 来源:网络整理
导读:#inherited在Foo语句之后被调用.我想要的东西只能在关闭类声明的结束语句之后运行. 这里有一些代码来说明我需要的内容: class Class def inherited m puts "In #inherited for #{m}" endendclass Foo puts "In Foo"endputs "I really wanted to have #inher
#inherited在Foo语句之后被调用.我想要的东西只能在关闭类声明的结束语句之后运行.

这里有一些代码来说明我需要的内容:

class Class
  def inherited m
    puts "In #inherited for #{m}"
  end
end

class Foo
  puts "In Foo"
end
puts "I really wanted to have #inherited tiggered here."


### Output:
# In #inherited for Foo
# In Foo
# I really wanted to have #inherited tiggered here.

有什么事情存在吗?可以创建吗我完全没有运气吗?

解决方法

你可能没有运气.但这只是一个警告,而不是一个明确的答案.

Ruby钩入类定义的开始,而不是结束,对于Class#继承的b / c ruby??类定义没有真正的结束.他们能
随时重新开放

有some talk a couple years ago about adding a const_added trigger,但还没有经历. From Matz:

I’m not going to implement every possible hook. So when somebody
comes with more concrete usage,I will consider this again. It would
be const_added,not class_added.

所以这可能会处理你的情况 – 但是我不确定(它最终可能在最终实现的时候触发).

你试图用这个触发器做什么?可能还有另一种办法.

(编辑:李大同)

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

    推荐文章
      热点阅读