Is there a hook similar to Class#inherited that's triggered only after a Ruby class definition?

前端 未结 8 2143
无人共我
无人共我 2020-12-03 13:35

#inherited is called right after the class Foo statement. I want something that\'ll run only after the end statement that closes the c

8条回答
  •  不知归路
    2020-12-03 14:24

    You may be out of luck. But that's only a warning, not a definitive answer.

    Ruby hooks the beginning of the class definition, rather than the end, for Class#inherited b/c ruby class definitions don't have a real end. They can be reopened any time.

    There was some talk a couple years ago about adding a const_added trigger, but it hasn't gone through yet. 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.

    So this might handle your case - but I'm not sure (it may trigger on the start too, when it's eventually implemented).

    What are you trying to do with this trigger? There may be another way to do it.

提交回复
热议问题