Skip to content

lbreguet/ruby-object-model

Repository files navigation

General Assembly Logo

Object Inheritance, Composition and Mixins in Ruby

Prerequisites

Objectives

By the end of this, students should be able to:

  • Give two reasons why composition and Mixins are desirable.
  • Write a class which inherits from another class.
  • Describe why composition is preferred to inheritance.

Preparation

  1. Fork and clone this repository.
  2. Install dependencies with bundle install.

JavaScript Inheritance

There is a Better Way (Composition)

Classical inheriterance has its limitations and we don't want to limit ourselves, that's where composition comes in. Let's discuss the code below:

class Car
  attr_reader :engine
  def initialize
    @engine = 1200
  end
end

class Ford < Car
end

focus = Ford.new
puts focus.engine

Whenever I create a new instance of a Ford, on initialization an instance of a Car is created as well. This instance of car has all of the methods defined in the Car class. It inherits all of methods defined in the Car class.

An Even Better Way (Mixins)

We want to make chunks of code that are resuable across of multiple classes. These "chunks" are called modules. Take a look at the code below:

module Sleeper
  def go_to_sleep
    # code here
  end
end

class Person
  include Sleeper
end

class Computer
  include Sleeper
end

In the code above we defined a module called Sleeper. We also define a Person class and a Computer class. By using the keyword include followed by the name of the module (in this case Sleeper) we have access to the methods we defined in our module. This is great because it allows us to keep our code D-R-Y, not to mention it allows us to be lazy developers (the good kind of lazy).

What should I use?

Inheritance creates a subclass - a class that has access to all of the methods of it's parent class. You should use it if your class is a type of it's parent class, like Ford is a type of car. A Ford is a more specialized, less abstract version of the Car class.

Mixins are used when a behavior is shared between various classes. People and computers both share the sleep behavior in the example above. People and computers are very different - it wouldn't make sense for them to inherit from the Sleeper class, so we use a module to share the behavior.

Lab: Model Shapes Using Classes

In a previous lab, you were asked to create and use a Shape class.

A Rectangle is a Shape, and a Square is a Rectangle.

Create a Rectangle that inherits from Shape. You will need to override the constructor method inside Rectangle to take two sides of different lengths. Since all rectangles have four sides, you can set a default value for @sides inside Rectangle's constructor.

Requirements for Rectangles:

  • Rectangles should be instantiated with Rectangle.new(3, 4) to create a rectangle with a length of 3 and a width of 4.
  • Instances of Rectangle should respond to the #area method and give the correct result.
  • Do not override anything that doesn't need to be overriden.

Next, create a Square class that inherits from Rectangle.

Requirements for Squares:

  • Squares should be instantiated with Square.new(4) to create a square with all sides equal to 4.
  • Instances of Square should respond to the #area method and give the correct result.
  • Do not override anything that doesn't need to be overriden.

Additional Resources

  1. All content is licensed under a CC­BY­NC­SA 4.0 license.
  2. All software code is licensed under GNU GPLv3. For commercial use or alternative licensing, please contact [email protected].

About

No description, website, or topics provided.

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages

  • Ruby 100.0%