Learn Ruby
Instance Method
20.1
Lesson

Instance methods on car

Let’s create a car instance using Carmaker.

Now that we have a car instance what can we do with it.

In a real car we can do things like “play music”, “stop car”, “turn lights on”. In our class we will have to add these methods to do those things. Let’s add a few methods to our class.

What’s a method. Previously we used methods like reverse, to_s and to_i. Methods like reverse, to_s and to_i are built into Ruby so we did not have to create those methods.

In Ruby, this act of adding a method in a class is called “defining a method”. So let’s define some methods. A method starts with keyword def and ends with the keyword end.

class Carmaker
  def play_music
  end

  def stop_car
  end

  def turn_lights_on
  end
end

These methods are empty and they don’t do anything. Let’s add a message in these methods.

class Carmaker
  def play_music
    puts "playing music"
  end

  def stop_car
    puts "stopping the car"
  end

  def turn_lights_on
    puts "turning the lights on"
  end
end

To use these methods we need to create a car instance and on that car instance we need to call the methods.

Run code to see the result.