Inheritance

Contents
Inherit with <
Call super
The ancestors chain
Override carefully

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

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 Dog < Animal def speak puts "Dog says:" super end end

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.

puts Dog.ancestors.inspect

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 Cat < Animal def speak puts "Meow" end end Cat.new.speak

Next: Enumerables

Article author: Arthur Isaev

Related articles
Ruby
Ruby course: zero to hero in 50 lessons
What is Ruby and where it runs
Install Ruby and check the version
irb: the interactive shell
Your first Ruby script
Variables
Numbers
Strings
Symbols
Truthiness: only false and nil
Conditions
Loops
Arrays
Hashes
Ranges
Methods
Blocks, procs, lambdas
String tools
Files and IO
Exceptions
Modules and mixins
Classes and objects
Inheritance
Enumerables
Duck typing and core idioms
Gems and Bundler
Testing with Minitest
Debugging
Encodings
Time and date
JSON and YAML
HTTP clients
Sockets
REST APIs
puts, print and p
Metaprogramming
Enumerators
Pattern matching
Arguments: splats and kwargs
Rake tasks
Environment and CLI
Logging
CSV
ERB templates
WEBrick server
Rails in one lesson
Capstone: todo CLI
Capstone: JSON API
Performance
Modern Ruby tour
Hero roadmap: the whole course on one page
Install Ruby on Windows 11
Install Ruby on Ubuntu
Install Ruby on Rocky Linux
Install Ruby on macOS
Install Ruby on FreeBSD
Ruby in Docker
Threads and fibers

Search this site

Channel @aofeed Chat @aofeedchat

Contacts and cooperation:
I recommend our hosting beget.com
Write to info@aredel.com if you:
1. Want to write an article for our site or translate an article into your native language.
2. Want to place thematically relevant ads on the site.
3. Ads on my site pass maximum censorship. If you see an ad block unsuitable for school-age children, shocking or misleading - please contact us by e-mail
4. Found a mistake, inaccuracy, bug, etc. on the site. ... .......
5. Articles can be shared on social media by clicking a network icon: