GithubHelp home page GithubHelp logo

khan-ngo / ruby-object-inheritance Goto Github PK

View Code? Open in Web Editor NEW

This project forked from ga-wdi-boston/ruby-object-inheritance

0.0 0.0 0.0 37 KB

Learn about inheritance in Ruby

License: Other

Ruby 100.00%

ruby-object-inheritance's Introduction

General Assembly Logo

Object Inheritence in Ruby

Prerequisites

Objectives

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

  • Diagram the Ruby method lookup chain
  • Write a class which inherits from another class.

Preparation

  1. Fork and clone this repository.
  2. Install dependencies with bundle install.
  3. Create a training branch and checkout to it.

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: 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 in lib/rectangle.rb 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.

Test your code with bin/rake test.

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 #calculate_area method and give the correct result.
  • Do not override anything that doesn't need to be overriden.

Next, create a Square class in lib/square.rb 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 #calculate_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].

ruby-object-inheritance's People

Contributors

samguergen avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.