Ruby Monstas


Classes & Objects

Pets

We describe pets by their attributes and behaviors.

All cats can be described by their name, color, and age. Those are called their attributes.

They can also meow, scratch, and sleep. Those are called their behaviors.

Cats

  • This is Ferdinand.
  • He is a a tabby cat. He is 3 years old.
  • He's an individual, but he shares attributes with other cats: all cats have a name, a color and an age.

Cats

  • This is Milo.
  • He is a a brown and white striped cat. He is 12 years old.
  • Just like Ferdinand, he has a name, a color and an age.

Cats are a Class

  • Since all cats have the same attributes, like name, color and age, we can define a class for them.
  • A class is a blueprint for creating objects.
  • Cats are a class of objects. Individual cats, like Ferdinand and Milo, are instances of the Cat class.

Code

Basic class definition


class Cat
  # Constructor method
  def initialize(name, color, age)
    @name = name      # instance variable for name
    @color = color    # instance variable for color
    @age = age        # instance variable for age
  end
end
       
          

Creation of Instances

  • To create a new instance of a class, we use the new method.
  • Each instance can have its own unique attribute values.
  • For example, we can create two cats with different names, colors, and ages.

The "initialize" Method

  • The initialize method is a special method in Ruby that is called when a new instance of a class is created.
  • It allows us to set initial values for the instance's attributes.
  • For example, when we create a new cat, we can provide its name, color, and age right away.
  • This is called "constructor" in other programming languages.

Initialize Cats

Basic class definition


class Cat
  # Constructor method
  def initialize(name, color, age) # this is the method that is called on new
    @name = name      # instance variable for name
    @color = color    # instance variable for color
    @age = age        # instance variable for age
  end
end

ferdinand = Cat.new('Ferdinand', 'tabby', 3)
milo = Cat.new('Milo', 'black and white striped', 12)


          

Getting our Cats to say hi!

class Cat
  # Constructor method
  def initialize(name, color, age)
    @name = name      # instance variable for name
    @color = color    # instance variable for color
    @age = age        # instance variable for age
  end
  def say_hi
    puts "Meow! My name is #{@name}, I am a #{@color} cat and I am #{@age} years old."
  end
end

ferdinand = Cat.new('Ferdinand', 'tabby', 3)
ferdinand.say_hi
          

-> "Meow! My name is Ferdinand, I am a tabby cat and I am 3 years old."

Asking Questions about our Cats

class Cat
  # Constructor method
  def initialize(name, color, age)
    @name = name      # instance variable for name
    @color = color    # instance variable for color
    @age = age        # instance variable for age
  end
  def old?
    if @age > 10
      true
    else
      false
    end
  end
end
ferdinand = Cat.new('Ferdinand', 'tabby', 3)
ferdinand.old?
milo = Cat.new('Milo', 'black and white striped', 12)
milo.old?
          

-> false / true

Local Variables

  • Local variables are variables that are defined within a method or block.
  • They are only accessible within that method or block.
  • For example, we can create a local variable to hold a cat's name.
  •  cat_name = "Whiskers"
    puts "The cat's name is #{cat_name}."
              

    -> "The cat's name is Whiskers."

Local Variables

  • If we try to access a local variable outside of its method or block, we will get an error.
  •  def print_cat_name
      cat_name = "Whiskers"
      puts cat_name
    end
    puts cat_name
              

    -> "undefined local variable or method `cat_name' for main:Object (NameError)"

Instance Variables

  • Instance variables are variables that are defined within a class and are accessible to all instance methods of that class.
  • They are prefixed with an @ symbol.
  •  class Cat
      def initialize(name)
        @name = name
      end
      def print_name
        puts "The cat's name is #{@name}."
      end
    end
    cat = Cat.new("Whiskers")
    cat.print_name
              

    -> "The cat's name is Whiskers."

Instance Variables

  • Instance variables are useable without definition (default value: nil).
  •  class MyClass
        def my_method
          @my_variable
        end
      end
    my_object = MyClass.new
    puts my_object.my_method
              

=> nil

Instance Variables

 class MyClass
  def initialize
    @my_variable = 12
  end
  def my_method
    @my_variable
  end
end
my_object = MyClass.new
puts my_object.my_method
          

=> 12

Questions?