0
def a_method p "in distress" end alias :hero :a_method def a_method hero p "Saved by the hero" end
这是一种遇险的方法,它已被英雄保存。但其他人不喜欢故事的结局,并决定覆盖英雄所做的事情。Ruby:重写别名
alias :nemesis ? def ? #which method to call? p "Captured by the nemesis" end
是否有可能克星覆盖主人公做了什么,有最后的结果是
"in distress" "Captured by the nemesis"
从我对别名工作的理解中,现在调用'hero'与调用原始方法基本相同。有没有'hero'修改原来的方法中的数据,我不能再得到的情况? – MxyL
Ruby是完全动态的;完全有可能随时更改方法,或者完全删除它,甚至使用运行时值重新定义它。所以你可以想象的任何情况可能*可能* ... –