如何在 Ruby 中向现有方法定义添加语句
我注意到对于类定义,如果我打开class MyClass
,并在中间添加一些内容而不覆盖,我仍然得到之前定义的原始方法。添加的新声明增强了现有声明。
但对于方法定义,我仍然想要与类定义相同的行为,但是当我打开 def my_method
时,def
中的现有语句和end
被覆盖,我需要再次重写。
那么有没有办法让方法定义的行为和定义一样,比如super
,但不一定是子类呢?
I noticed for the class definition, if I open up the class MyClass
, and add something in between without overwrite I still got the original method which defined earlier. The new statements added augment the existing one.
But as to the method definition, I still want the same behavior as the class definition, but it seems when I open up the def my_method
, the exiting statements within the def
and end
is overwritten, I need to rewrite that again.
So is there any way to make the method definition behave the same as definition, something like super
, but not necessarily is the sub-class?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我想您正在寻找 alias_method:
I suppose you are looking for alias_method: