RubyGuides
Share this post!

Understanding Ruby Methods & Writing Your Own

What is a Ruby method?

A method is made of lines of Ruby code grouped together with one specific goal.

The goal can be to:

  • GET information
  • CHANGE or CREATE objects
     

Example 1:

The size method on an Array object gives you a count of elements (information).

Example 2:

The pop method removes the last element from the array (change).

When you understand the relationship between objects, classes & methods everything starts to make sense.

relationship between objects, classes and methods

Let’s keep learning!

How to Define Methods

Ruby has many powerful built-in methods you can use, but you can also create your own.

How?

You can define your own Ruby method using the def keyword.

Here’s the syntax:

def gimme_bacon
  puts "Bacon plz."
end

What’s going on here?

  1. def is part of Ruby’s syntax, it says that we want to define a method
  2. gimme_bacon is the name of the method
  3. puts "Bacon plz." is the body of the method
  4. end marks the end of the method definition

Defining a method only tells Ruby that you want to create it.

If you want to use it then you need to call the method.

How to Call Methods in Ruby

In Ruby, when we use a method, we say that we’re calling it.

You’ll often hear “method call”, or if you’re working with someone who’s an Object-Oriented purist, you may hear that “you’re sending a message”.

Either way…

Let’s see an example of using a method.

Here you go:

gimme_bacon

This prints:

"Bacon plz."

You can call methods on objects.

For example:

n = [1,2,3]

n.size
# 3

This n.size is calling the method size on the object n, which happens to be an Array.

The result?

We get the array’s size.

What methods are available?

That depends on the class of the object you’re calling the method on.

An array is going to have different methods than a hash.

You can check the Ruby documentation to find a list of methods for a given class.

Returning Values From Methods

One key concept in Ruby is that ALL methods return a value.

Let me explain!

As a result of calling a method, you get something back.

This “something” that you get comes from the last expression in your method definition.

Here’s what I mean:

def number_one
  1
end

number_one
# 1

Another example:

def add(x,y)
  x + y
end

add(5, 6)
# 11

We call this “implicit return”, just a fancy name for “automatically return the last thing”.

In addition:

You can tell Ruby to return something with a keyword.

def two
  return 2
end

# 2

Notice that your method stops running when you use return.

You use this is for an early return in your code, or to exit a loop.

Why is There a Question Mark in My Method Name?

You may find some strange Ruby methods.

With names like:

  • empty?
  • sort!
  • title=

ALL of these are valid method names.

What does the question mark, the exclamation mark, or the equals sign mean?

They are conventions in the Ruby community.

Explanation:

  • A question mark method, also known as a predicate method, is supposed to return either true or false
  • An exclamation mark method says that it will do something different than the non-exclamation mark version. Usually, this is associated with changing the object itself in some way (adding / deleting elements)
  • The equals sign method means assignment. It’s used to assign a value to an instance variable

None of these conventions are enforced by the language.

But…

If you follow them, you’ll be able to write more Ruby-like code!

Summary

You’ve learned about the power of Ruby methods, how to define them, use them & how to follow proper conventions.

Now it’s your turn to put this into practice 🙂

Thanks for reading!

Leave a Comment:

2 comments
Kingdavid says last week

clear explanation! thanks man

Reply
    Jesus Castello says last week

    Thank for reading 🙂

    Reply
Add Your Reply