Classes

Every object has a class definition. The built in class definitions include String, Integer, Array.

Create custom classes with the class keyword

Class structure

All classes inherit from BasicObject there is no need to implicitly state this.

Some key rubyisms:

# Definition of an Animal Class
class Animal
    # Shortcut to create a getter for variable
    attr_reader :typeOfBeast

    # Constructor
    def initialize
        # @ means instance variable
        @typeOfBeast = "Animal"
        @legs = 4
        # Can't access @heads outside of this class
        @heads = 1
    end

    # Getter for legs private instance variable via method
    def legs
        @legs
    end

    def tails
        @tails
    end

    # Protected function tails not accessible outside of class
    private :tails
end

Class Structure Example

This script will fail with the error that undefined method 'heads'. See if you can fix it 🛠

$ ruby examples/class-structure.rb

#<Animal:0x00007fa0eb077108 @typeOfBeast="Animal", @legs=4, @heads=1>
Animal
4 legs
Traceback (most recent call last):
examples/class-structure.rb:26:in `<main>': undefined method `heads' for #<Animal:0x00007fa0eb077108> (NoMethodError)

Inheritance

Ruby supports single inheritance. Multiple inheritance is not supported

# Generic Animal class
class Animal
    attr_reader :typeOfBeast
    # use protected keyword to prevent direct access
    protected :typeOfBeast
    def initialize
        @typeOfBeast = "Animal"
    end
    def whichType
        # Protected attribute available here
        @typeOfBeast
    end
end

# Specific Dog class
class Dog < Animal
    attr_accessor :typeOfAnimal
    def initialize
        # Call to Animal constructor
        super
        @typeOfAnimal = "Dog"
    end
end

Class Inheritance Example

$ ruby examples/inheritance.rb

A Dog is a type of Animal

TL;DR

Sites

Pragmatic Ruby - classes, variables and methods Ruby Monk - objects chapter

Videos

Ruby Fundamentals - Class methods and variables

Ruby Fundamentals - Method visibility

Challenge 🎠

Use Rspec to run the challenges

bin/rspec spec/challenges/classes_spec.rb --format doc

fix ../lib/challenges/challenges/classes.rb so the tests pass ✔️

Go to next challenge

Flow Control