我有以下类(class)
class Animal
def move
"I can move"
end
end
class Bird < Animal
def move
super + " by flying"
end
end
class Penguin < Bird
def move
#How can I call Animal move here
"I can move"+ ' by swimming'
end
end
如何在 Penguin 中调用 Animal 的移动方法?我不能使用 super.super.move。有什么选择?
谢谢
最佳答案
可以获取Animal
的move
实例方法,绑定(bind)到self
,然后调用:
class Penguin < Bird
def move
m = Animal.instance_method(:move).bind(self)
m.call
end
end
关于ruby-on-rails - 如何在 Ruby 中调用 super.super 方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28058336/