Learn
On the flip side, sometimes you’ll be working with a derived class (or subclass) and realize that you’ve overwritten a method or attribute defined in that class’ base class (also called a parent or superclass) that you actually need. Have no fear! You can directly access the attributes or methods of a superclass with Ruby’s built-in super
keyword.
The syntax looks like this:
class DerivedClass < Base def some_method super(optional args) # Some stuff end end end
When you call super
from inside a method, that tells Ruby to look in the superclass of the current class and find a method with the same name as the one from which super
is called. If it finds it, Ruby will use the superclass’ version of the method.
Instructions
1.
We decided we want to do some chops-punching after all! Let’s do two things:
- Add
puts "Instead of breathing fire..."
as the first line in ourDragon
‘sfight
method. - Replace the return statement inside
Dragon
‘s definition offight
with the keywordsuper
. (No need to pass any arguments tosuper
, sinceCreature
‘sfight
method doesn’t take any.)
Take this course for free
By signing up for Codecademy, you agree to Codecademy's Terms of Service & Privacy Policy.