Inheritance
Inherit with <
Write the child class name, then the < sign, then the parent class name. The child receives every method of the parent and can add its own.
If classes are new to you, start with
Classes and objects.
class Animal def speak puts "Some sound" end end class Dog < Animal end Dog.new.speak
ruby inherit_demo.rb
Some sound
Call super
When a child defines a method with the same name, the child version wins. Call super inside it to run the parent version as well.
Plain super passes the same arguments along. Add explicit arguments in parentheses when the parent needs different ones.
class Animal def speak puts "Some sound" end end class Dog < Animal def speak puts "Dog says:" super end end Dog.new.speak
ruby super_demo.rb
Dog says:
Some sound
The ancestors chain
Every class remembers its full lookup path. Ask ancestors to see the class itself, then included modules, then the parent, up to Object and BasicObject.
Ruby walks this list from left to right when you call a method, and the first match runs. For Dog it prints Dog, Animal, Object, Kernel, BasicObject.
class Animal end class Dog < Animal end puts Dog.ancestors.inspect
ruby ancestors_demo.rb
[Dog, Animal, Object, Kernel, BasicObject]
Override carefully
Override only when the child truly behaves differently. Keep the method name and argument shape so old callers keep working.
If the parent method does useful work, call super instead of copying its code. Duplicated logic drifts apart over time.
class Animal def speak puts "Some sound" end end class Cat < Animal def speak puts "Meow" end end Cat.new.speak
ruby override_demo.rb
Meow
Questions
A new set of questions in a new order every time. Choose one answer for each, then check.
-
What does
class Dog < Animaldeclare?Answer
Dog inherits from Animal
The child receives the methods of the parent and may add or override its own.
-
What does this program print?
class Animal def speak puts "Some sound" end end class Dog < Animal end Dog.new.speakAnswer
Some sound
Dog inherits speak from Animal, so the call finds it in the parent.
-
What does this program print?
class Animal def speak puts "Some sound" end end class Dog < Animal def speak puts "Dog says:" super end end Dog.new.speakAnswer
Dog says: / Some sound
The child prints its line, then super runs the parent method.
-
What does this program print?
class Animal end class Dog < Animal end puts Dog.ancestors.inspectAnswer
[Dog, Animal, Object, Kernel, BasicObject]
The class comes first, then the parent, and Kernel is the module mixed into Object.
-
When a method is defined in several classes of the ancestors chain, which one runs?
Answer
The first match walking the chain from left to right
Ruby walks ancestors from left to right and stops at the first match.
-
What does this program print?
class Animal def speak puts "Some sound" end end class Cat < Animal def speak puts "Meow" end end Cat.new.speakAnswer
Meow
The child version wins; the parent version runs only when the child calls super.
-
What does a plain
superinside a method pass to the parent?Answer
The same arguments the method received
Explicit parentheses are the way to pass different arguments.
-
What does this program print?
class A def x 1 end end class B < A end p B.new.xAnswer
1
The method is found on A, the parent of B.
-
A child overrides
speakand does not call super. What is lost?Answer
The work the parent method did, unless the child repeats or calls it
The override replaces the parent method entirely for that class.
-
What does this program raise?
class A end class B < A def x super end end B.new.xAnswer
NoMethodError: super: no superclass method 'x'
super looks for the same method in the parent, and A has none.
Article author: Arthur Isaev