Steve Loar

Web developer, horse rider and maker of small tables.

Ponies at Lost Valley Ranch

Ruby Classes

Classes are an important component of Ruby programming. They can be used to model real objects that have properties and behavior. In Ruby the properties are called instance variables. The behavior happens in methods. With these, one can simulate almost anything.

When a new class is instantiated a new object is created. The class will have a method that is run when this happens. This is the initialize method. Parameters can be passed to this method and in its code, instance variables can be set.

The other methods of a class can be public or private. Public methods can be accessed outside the class. Private methods can only be accessed from inside the class.

Let me give an example of this to illustrate these principles. Let's say I wanted to model a horse. I would give the horse instance variables for name, color, breed, and mood. When I create a new horse I would pass the specific values of the horse I was modeling.

# Our horse class
Class Horse
  def initialize(name, breed, mood)
    @name = name
    @breed = breed
    @mood = mood
  end 
end

# instantiate our horse 
rico = Horse.new("Rico, "Quater Horse", "sleepy")

We now have our horse, rico, but he can't do anything yet. So we need to add some methods. Let do that now. We will give the horse class some public methods that we, as the rider, can access. And we'll create some private methods that only the horse can access.

# Our horse class
Class Horse
  def initialize(name, breed, mood)
    @name = name
    @breed = breed
    @mood = mood
  end 

  def ask_to( something )
    walk if something = "walk"
    trot if something = "trot"
    canter if something = "canter"
    stop if something = "whoa"
  end

  def talk
    puts "Hi my name is #{@name}. I am a {#mood} {#breed}."
  end

  def give_treat
    @mood = "happy"
  end 

  private

  def walk
    puts "I'm walking."
  end

  def trot
    if @mood == "happy"
      puts "I'm trotting. Don't we look smart."
    else
      walk
    end 
  end

  def canter
    if @mood == "happy"
      puts "I'm cantering. Hold on!"
    else
      trot
    end 
  end

  def stop
    puts "Ok, I'm stopped."
    @mood = "sleepy"
  end

end

# instantiate our horse 
rico = Horse.new("Rico, "Quater Horse", "sleepy")
# ask rico to walk
rico.ask_to("walk") #=> gives "I'm walking."
rico.ask_to("trot") #=> gives "I'm walking." What??
rico.talk #=> gives "Hi my name is Rico. I am a sleepy Quarter Horse."
# Let's fix that
rico.give_treat
rico.ask_to("trot") #=> gives "I'm trotting. Don't we look smart." That's better!

From this example we can see that our horse, rico will walk for us when we ask. But he will not trot for us and we cannot make him, as that is a private method that he controls. However, if we give him a treat, his mood will change and we can ask him to trot and he will do so. Notice that when we ask him to stop, his mood will change back to sleepy again!