What is a Ruby method?
A method is a set of one or more lines of Ruby code that are grouped together for a specific purpose.
The grouped code is given a name for easy reuse without the need for rewriting or copying & pasting the code.
The purpose of a method can be to:
- GET information.
- CHANGE or CREATE objects.
- FILTER & FORMAT data.
size method on an
Array object gives you a count of elements (get information).
pop method removes the last element from the array (change objects).
When you understand the relationship between objects, classes & methods everything starts to make sense.
Let’s keep learning!
How to Define Methods
The Ruby programming language has many powerful built-in methods you can use, but you can also create your own.
You can define your own instance method using the
Here’s the syntax:
def gimme_bacon puts "Bacon plz." end
What’s going on here?
defis part of Ruby’s syntax, it says that we want to
define a method
gimme_baconis the name of the method
puts "Bacon plz."is the body of the method
endmarks 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”.
Let’s see an example of using a method.
Here you go:
You can call methods on objects.
n = [1,2,3] n.size # 3
n.size is calling the method
size on the object
n, which happens to be an
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.
How to Return Values (Basic Syntax)
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
def add(x,y) x + y end add(5, 6) # 11
We call this an “implicit return”, just a fancy name for “automatically return the last thing”.
You can tell Ruby to
return something with a keyword.
def two return 2 end # 2
Notice that unlike other languages this return statement is NOT required & your method will stop running as soon as it finds
You can use this is for an early return (guard statements are good example), 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:
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.
- A question mark method, also known as a predicate method, is supposed to return either
- 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, like adding / deleting elements. Also known as “bang methods”.
- 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.
If you follow them, you’ll be able to write more Ruby-like code!
You’ve learned about the power of Ruby functions, how to define them, use them & how to follow proper conventions.
Now it’s your turn to put this into practice 🙂
Thanks for reading!