All original content is created in Ukrainian. Not all content has been translated yet. Some posts may only be available in Ukrainian.Learn more

What is inheritance in Ruby? Examples of bad and good inheritance.

Table of contentsClick link to navigate to the desired location
This content has been automatically translated from Ukrainian.
In Ruby, as in many other object-oriented languages, inheritance is a mechanism by which a class can inherit properties and methods from another class. The class that inherits is called a subclass, and the class from which it inherits is called the superclass.

Example of Bad Inheritance in Ruby

class Animal
  def speak
    puts "Animal speaks"
  end
end

class Dog < Animal
  def speak
    puts "Dog barks"
  end
end

class Cat < Animal
  # This class does not define its own speak method
end
In this example, the Dog class inherits from the Animal class, but the Cat class does not provide its own implementation of the speak method. If you try to create an instance of the Cat class and call the speak method, it will use the implementation from the Animal class. This can be problematic, as the appropriate response for a cat is meow
That is, we expect to get the text 'Cat meows', but we get 'Animal speaks'. It may seem not very critical, but in classes responsible for business logic, this can lead to bigger problems. 
We can do the following:
class Cat < Animal
  def speak
    puts "Cat meows"
  end
end
This seems to solve the problem. But imagine there are dozens or hundreds of classes and methods. This approach is not a best practice.

Example of Good Inheritance in Ruby

class Shape
  def initialize(color)
    @color = color
  end

  def draw
    puts "Drawing a #{@color} shape"
  end
end

class Circle < Shape
  def initialize(color, radius)
    super(color)
    @radius = radius
  end

  def draw
    puts "Drawing a #{@color} circle with radius #{@radius}"
  end
end

class Square < Shape
  def initialize(color, side_length)
    super(color)
    @side_length = side_length
  end

  def draw
    puts "Drawing a #{@color} square with side length #{@side_length}"
  end
end
In this example, there is a Shape class, which is the base class for all geometric figures. The class has a color attribute and a draw method that outputs a drawing message.
The Circle and Square classes inherit from the Shape class and provide their own attributes (radius and side_length) and implementation of the draw method. Each class can call the draw method, but with its own context and data.
This approach allows for creating common properties and methods in the base class, and then extending them in subclasses, providing their own unique logic. This makes the code more readable, maintainable, and flexible.

This post doesn't have any additions from the author yet.

24 Jul 12:17

How to make Counter-Strike: GO fullscreen in Ubuntu without launching the game?

meme code
meme code@memecode
20 Nov 19:35

How to recover a deleted file (Trix) that was stored on Amazon S3?

meme code
meme code@memecode
29 Nov 08:47

What is the difference between var and let in Javascript?

meme code
meme code@memecode
07 Dec 07:42

What is encapsulation in OOP?

meme code
meme code@memecode
07 Dec 08:13

Visibility control in Ruby (public, private, and protected)

meme code
meme code@memecode
07 Dec 08:25

What is OOP (object-oriented programming)?

meme code
meme code@memecode
09 Dec 12:15

What is best practice in programming?

meme code
meme code@memecode
09 Dec 12:21

What is polymorphism? An example of using polymorphism in Ruby.

meme code
meme code@memecode
09 Dec 12:46

Що таке патерн/шаблон проєктування?

meme code
meme code@memecode
10 Dec 14:03

What is a design pattern in programming?

meme code
meme code@memecode
10 Dec 14:18

What is a client and a server? What is the interaction mechanism between the client and the server?

meme code
meme code@memecode
18 Dec 08:25

What is DNS? What is DNS used for?

meme code
meme code@memecode