- Read Tutorial
- Watch Guide Video
In this guide we will examine the various ways to pass arguments to methods in Ruby programs, including:
- The argument syntax
- Named arguments
- Default argument values
What are method arguments?
Before we can get into the code examples let's first walk through what method arguments are. Let's begin with a real world example.
Imagine that you have a machine that makes baseball bats. The workflow for the bat making process would be:
- The raw wood is placed in the machine.
- From there the machine takes the wood, cuts and polishes it.
- Lastly it finishes off by outputting the finished baseball bats from the machine.
So let's see how this analogy applies to methods in Ruby:
- Method Arguments - the raw wood placed inside the machine are the method arguments. This is data that can be provided by a user, a database query, an API, etc. It is rare for a method to not have arguments since method arguments are what allows for dynamic behavior. Looking back at our example, would it be possible to produce the baseball bats if we didn't first supply the machine with the raw materials? Of course not. In the same way methods need data in order to work with.
- The Method - the machine itself represents the method. This is where the actual logic goes that will produce the desired behavior.
- The returned values - lastly the finished bats are like the values that get returned by methods.
Method Argument Syntax
Now that you have a good idea on how methods work, let's walk through how we can pass arguments to them.
Method Argument Code Examples
I'm going to start with a very basic example that prints out a user's full name. This is actually a code snippet I took from a real world code project.
def full_name(first_name, last_name) first_name + " " + last_name end puts full_name("Jordan", "Hudgens")
As you'll see this prints out my full name.
The syntax for how to pass arguments to a method is to simply list them off on the same line that you declare the method. From here you can treat the names that you supplied as arguments (in this case first_name
and last_name
) as variables in the method.
Now there is also an alternative syntax that we can use that's quite popular among Ruby devs for passing arguments to methods. Here is a slightly updated version of the code example from before:
def full_name first_name, last_name first_name + " " + last_name end puts full_name "Jordan", "Hudgens"
Notice how all of the parenthesis have been taken away? There is some debate on which option is better. I personally prefer to write less code when I have the chance to, so I prefer the second syntax. However both options will work well.
Named Arguments
Additionally Ruby gives us the ability to name our arguments. This can be helpful when you have method signatures with a number of arguments and you want the method calls to be explicit. Here is how you can use named arguments in Ruby:
def print_address city:, state:, zip: puts city puts state puts zip end print_address city: "Scottsdale", state: "AZ", zip: "85251"
If you run this code it will work properly.
So why are named arguments helpful? I think the easiest way to answer that question is to update our code and remove the named components. That code would look like this:
def print_address city, state, zip puts city puts state puts zip end print_address "Scottsdale", "AZ", "85251"
If you run this code it will work exactly like before.
However by removing the named arguments we've made our method more difficult to work with and more prone to bugs. When working with a method as simple at print_address
it's easy to know what the parameters city
, state
, and zip
represent and to provide them in that order. However what if we had a method that looked like this:
def sms_generator api_key, num, msg, locale # Magic SMS stuff... end
In a case like this we would have to reference the method declaration several times to ensure we're passing in the arguments properly. And what would happen if we tried to call this method with the code:
sms_generator "82u3ojerw", "hey there", 5555555555, 'US'
Nothing looks wrong with this code, right? Actually this code won't work because we've accidentally swapped the order of the phone number and message. In a real application the method would try to send the SMS message to the string hey there
, which wouldn't work for obvious reasons.
However if we update this method to use named arguments, the order no longer matters:
def sms_generator api_key:, num:, msg:, locale: # Magic SMS stuff... end sms_generator api_key: "82u3ojerw", msg: "hey there", num: 5555555555, locale: 'US'
When you utilize named arguments you don't have to worry about the order that you pass the arguments in, which is a nice convenience and will also help prevent bugs in your program.
Default Argument Values
We'll finish up our discussion on Ruby method arguments by discussing default values. There are many times when you'll want to supply default values for an argument and Ruby let's you implement this functionality quite easily.
Let's take the example of building a method that streams movies. We'll need the user to tell the method which movie to watch, however we don't want them to have to enter in the language for the movie unless it's different than English. In cases like this we can use default values with this syntax:
def stream_movie title:, lang: 'English' puts title puts lang end stream_movie title: 'The Fountainhead'
As you can see this will work properly and prints out English
as the language because we declared it as a default.
And now if we want to watch a movie in Spanish we can optionally pass in the lang
argument into the method call:
stream_movie title: 'The Fountainhead', lang: 'Spanish'
I hope that this has been a helpful guide to how you can use method arguments in Ruby. In the next guide we'll walk through the splat and optional argument components that Ruby allows.