All About Ruby Arguments (and Parameters)

Evan Fujita
3 min readJan 4, 2021

Arguments vs Parameters

While the term ‘argument’ can be used in place of ‘parameter’ (and it often is), the distinction between the two is worth noting. If an argument is a value that is passed into a method, then the parameter is simply a placeholder for the argument in the definition of the method. We can boil down the differences to this:

  • Parameters have no inherent value and are established with the definition of a method;
  • Arguments contain a value and are passed into a method.

If you’ve ever done mad libs, parameters and arguments work in a similar way: the parameter is the blank space, suggesting that something needs to go there, and what eventually fills that blank space is the argument.

Yep, just like this! Sort of…

Defining a Parameter

When defining a method, the parameter(s) will be identified in two places: alongside the method name and within the method body. First, let’s look at how to include a parameter in a method definition:

def method(parameter)      
parameter
end

This is a simple example of a method with a parameter. As mentioned above, the parameter is defined alongside the method name and within the method body, but what else can be said about this method? The parameter here represents the return value of the method. This is where ‘argument’ comes in: when we call this method, we will be substituting the parameter with our argument and, accordingly, the argument passed in will be the return value:

method(“Hello”)     #=> “Hello”

— -

What else can we do with parameters and arguments? To start, we can add more! Notice in the following example we have more specific parameters to help us better understand the purpose of the method…

def greeting(name, birthplace, favorite_food)
puts “Hello, my name is #{name}. I'm from #{birthplace} and I love #{favorite_food}!”
end

Now we can call the method and pass in our arguments:

greeting(“Evan”, "Chicago", “pizza”)     
#=> Hello, my name is Evan. I'm from Chicago and I love pizza!

— —

We can also make default arguments:

def greeting(name, birthplace, favorite_food = "sushi")
puts “Hello, my name is #{name}. I'm from #{birthplace} and I love #{favorite_food}!”
end

Using this, if we pass in two arguments, both will be represented in the code. However, if we pass in only one argument, then we’re still okay!

greeting(“Evan”, "Chicago")     
#=> Hello, my name is Evan. I'm from Chicago and I love sushi!

**Let me point out that there’s only one default argument here: favorite_food. As long as a parameter does not have a default argument, we must pass in an argument when calling the method.

— —

Up until this point our scope has been rather narrow. We’ve seen arguments as strings, but they can actually be any data type. Let’s explore this a little.

So let’s say we defined an array x = [1, 2, 3, 4]. Then using our first method, we will see:

method(x)
#=> [1, 2, 3, 4]

That’s useful, but here’s a more complicated example that illustrates how arguments will allow us to access more information:

def description(cat)
puts cat.name
puts cat.age
puts cat.type
end
class Cat
attr_accessor :name, :age, :type
def initialize(name, age, type)
@name = name
@age = age
@type = type
end
end
winston = Cat.new(“Winston”, 2, “American Shorthair”)

Okay, so we’ve got here:

  1. #description method that has a parameter of ‘cat’ and will puts out information based on the argument passed in;

2. a Cat class that initializes name, age, and type;

3. a single Cat instance of ‘winston’ with attributes assigned to him.

(And notice how we’re assigning these attributes to ‘winston’… look familiar?!)

The result:

description(winston)#=> 
Winston
2
American Shorthair

The main takeaway here is that ‘winston’ is an object that is assigned attributes, and that when ‘winston’ is passed as an argument, all of the attributes follow, too. So while we can make a simple method that accepts a simple argument of a string and puts it out, the usage of arguments and parameters actually becomes more involved, and more useful, when we start passing through objects that carry more data, allowing us to access and work with that information.

--

--