General Assembly Logo

Object Inheritence in Ruby (versus JavaScript)

Prerequisites

Objectives

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

  • Diagram the Ruby method lookup chain, and compare it to the JavaScript prototype lookup chain.
  • Write a class which inherits from another class.

Preparation

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

JavaScript Inheritance

Remember how JavaScript method lookup works? We use prototypes. Here's an example:

User.prototype.totalDistance = function() {
  let result = 0;

  for (let i = 0; i < this.runs.length; i++) {
    result += this.runs[i].distance;
  }

  return result;
}

We defined methods by attaching them to the prototype of the User object. New instances of User will "inherit" their methods from the User prototype. In this example, all instances of User share the common property totalDistance.

Ruby Inheriterance

In Ruby, we have classes instead of prototypes. We attach methods to the class since Ruby doesn't use prototypes. Ruby will be our first example of linking different objects together in a hierarchy. This process is called "inheritance" and the result is what we mean when we say an object "inherits" behavior from another object.

Some objects can be classified in multiple ways. These multiple classifications often make sense as a hierarchy. For example, a Dog is a kind of Pet. It's also a kind of Animal. In Ruby, we can share code (data or behavior) between two classes using inheritance. Let's look at an example of inheritance. Note that a Ruby class can only inherit from one other class, so whether you name that class Pet or Animal will depend on your application.

class Animal
  def eat
    puts "Nom nom nom"
  end
end

class Dog < Animal
end

dog = Dog.new
dog.eat #=> "Nom nom nom"

class Dog < Animal
  def speak
    puts "WOOF"
  end

  def eat
    puts "Slop slop slop"
  end
end

dog.speak #=> "WOOF"
dog.eat #=> "Slop slop slop"

animal = Animal.new
animal.eat #=> "Nom nom nom"
animal.speak #=> NoMethodError

Ruby: Super Inheriterance

class Animal
  def move
    "I can move"
  end
end

class Bird < Animal
  def move
    super + " by flying"
  end
end

puts Animal.new.move
puts Bird.new.move

This will print out:

I can move
I can move by flying

Super will call the same method defined in the parent or superclass and give you the result.

Demo: Drawing the Method Lookup Chain in Ruby and JavaScript

In Ruby, method lookup occurs through classes. In JavaScript, method lookup occurs through inspecting the .prototype property on constructor functions.

Let's draw the method lookup chain, first through prototypes in JavaScript, and then through classes in Ruby.

Lab: Drawing the Method Lookup Chain in Ruby

Please diagram the method lookup chain using the following requirements:

  • The class DenverBroncos has an instance method called lose.
  • DenverBroncos inherits from the class FootballTeam.
  • The class FootballTeam has an instance method called play_game.
  • Diagram creating a new instance of the DenverBroncos: broncos_2015 = DenverBroncos.new.
  • Diagram how Ruby finds and executes the methods called on broncos_2015: broncos_2015.lose and broncos_2015.play_game.

Lab: Creating Your Own

Using the requirements above inheritance to write the code in lib/broncos.rb

Additional Resources

Source code distributed under the MIT license. Text and other assets copyright General Assembly, Inc., all rights reserved.