I have the following classes
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
How can I call Animal's move method inside Penguin ? I can't use super.super.move. What are the options?
Thanks
You can get the move
instance method of Animal
, bind it to self
, then call it:
class Penguin < Bird
def move
m = Animal.instance_method(:move).bind(self)
m.call
end
end
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With