• 20 hours
  • Easy

Free online content available in this course.



Got it!

Last updated on 2/6/20

Create Ruby methods to perform actions in your code

Log in or subscribe for free to enjoy all this course has to offer!

So far in this course, you've created a class called Person and instances of that class (people like Arnaud and Emma) that have attributes like a name, an age, and an outfit. Nice!

At a certain point in your code though, you'll want the objects you create in Ruby to do things. I mentioned the example of creating an instance of the Carclass earlier in this course.  If you create a new car as follows:

my_car = Car.new
my_car.make = "Jeep"
my_car.model = "Cherokee"
my_car.year = 2017
my_car.color = "red"

I want this car that I create to be able to drive forwards, backwards, park, and more. 

This is why Ruby allows you to write methods!

What is a method?

Methods are like verbs for your code!

Consider the English language: your sentences are primarily made up of nouns, adjectives, and verbs. In Ruby, you already know how to create the equivalents of "nouns" (like a car or a person) and adjectives (like red) or descriptors like someone's age or name.

Ruby lets you define your own custom methods and has many predefined methods that will make your coding life easy.

We'll re-use our Person example from the first part of this course. Let's give our people the ability to perform two actions:

  • say hello

  • say goodbye

Defining a method

It's time to write a method of your own. Follow along in this interactive code exercise:


To create a method in Ruby, you always start with a keyword called def,followed by the name of the method you want to create (def to indicate you will be defining a method)!

As with a class, you always close your method definitions with the keyword end. The outline of your method definition will therefore look like this:

def say_hello

Within these bookends, you can write what you want your method to do. I always like to jot out what I want my method to do as a note to myself; it helps me think through the information I need and the actions I need to perform. 

def say_hello
    # i want this method to put the person's name plus the text "says hello!"

This note to myself is called a code comment and is greyed-out. It won't actually run as part of my program. Comments are a great way to give clarifications about your code, leave notes to yourself, etc (though leave them in your final code only when you have to)!

By jotting down what I want this method to do, I know I need:

  • access to the person's name

  • a way to combine their name with additional text ("says hello!")

Excellent. Let's get cracking on accessing this information.

Instance variables

You're defining this method as part of the Person class. Within class definitions in Ruby, you have access to these groovy little things called instance variables

"Hey! I already know how to access a person's name. In the previous chapter, we usedperson.name! Why can't I just get the person's name using that?"

True! You did access a person's name that way.

However, you can run person_1.name to see the contents of a particular person's name outside of the class definition.

From within a class definition (which is where you'll define methods that are available to a particular class), if you want to access an object's attribute, you'll access it via instance variable.

Instance variables are written with an @ sign. Here are some examples:


Within thesay_hellomethod definition, I can therefore add the instance variable, while leaving the second part of my note to myself:

def say_hello
    @name # plus the text "says hello!"

It turns out that adding the text "says hello!" in my Ruby method matches almost exactly as I wrote it in my note to myself. To add two texts together, you can simply join them with a + sign. 

def say_hello
    @name + "says hello!"

Now, if I call my new method on a person, the result might look like this:

=> "Arnaud says hello!"

Ta-da! A complete sentence with the name of our person in it. If I called it on a different person, the result would look like this:

=> "Emma says hello!"

For practice, try writing a method say_goodbye that does the same thing, except with goodbye instead of hello!

Example of certificate of achievement
Example of certificate of achievement