Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to call super.super method in Ruby

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

like image 783
Toan Nguyen Avatar asked Dec 06 '22 23:12

Toan Nguyen


1 Answers

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
like image 190
August Avatar answered Dec 22 '22 04:12

August