Ruby Required, Default and Optional Parameters

Getty Orawo
podiihq
Published in
4 min readApr 4, 2018

So Hey, ever bumped into the term Parameters in Ruby, Well parameters are often mistaken with the term arguments. Today I have the pleasure of dawning reality on you. They are similar, in a not so similar way lol. So thing is, there’s a thin line between the two. See in the example above, we define a method called print_sum that takes two parameters. Understand that we shall later on call this method and pass it values for the corresponding parameters(arg1, and arg2), those values are arguments.

We defined it with parameters but pass it arguments when calling it

So parameters are simply variables used within a given method definition while arguments are the corresponding values of those parameters. Note that parameters are used during a method definition while arguments are used during a method call.

Types of parameters

There are three types of parameters in Ruby:

  • Required Parameters
  • Default Parameters
  • Optional Parameters

Required parameters

These are parameters that have to be provided with an argument or a value during method call. When calling a method, if no argument is passed it throws an error.

def chocolate(number_of_milk_packets)
“This will be good with #{number_of_milk_packets} packets”
end

The method defined above requires the parameter number_of_milk_packets. If we call it without giving it any arguments, we get an error that claims we gave the method no argument but it instead expects one argument.

Think of it like this.. We must have at least some packets of milk to have chocolate otherwise no chocolate for you

chocolate() #calling the method with no argument===> ArgumentError: wrong number of arguments (given 0, expected 1) #throws ArgumentError

This time lets give it more than one argument, guess what happens…? Take a guess… You’re probably right, it’s an error!, expecting 1 but we gave it 3

chocolate(1,2,3) #calling the method with more than one argument===> ArgumentError: wrong number of arguments (given 3, expected 1) #throws ArgumentError

So what this error means is that we gave the method three arguments but it expects only one argument, hence the argument error.

Default parameters

Default parameters as their name suggests, basically set a default value in case none is provided. There’s always a fallback option with these parameters. I’ll use the example above but with a default parameter instead.

example…

def chocolate(number_of_milk_packets=1) #method with default parameter
“This will be good with #{number_of_milk_packets} milk packets”
end

calling the method above without an argument uses the initially provided default argument instead.

chocolate() #calling the method with no argument===> This will be good with 1 milk packets

But as below, if you give it an argument, it uses that argument instead of the default argument provided.

chocolate(7) #calling the method with an argument===> This will be good with 7 milk packets #takes the given value

Optional parameters

Optional parameters are the most interesting of them all since they aren’t always limited to a specific number of arguments. Once I declare an optional parameter on a method definition, I can comfortably use an unlimited number of arguments during a method call. All the values provided during method call are placed into single array.

example…

def chocolate(number_of_milk_packets*) #with optional parameter
“This will be good with #{number_of_milk_packets} packets”
end

Optional parameters place all provided arguments within an array rather than throwing an ArgumentError.

chocolate(5,9,2) #method call with three arguments for optional parameter===> “This will be good with [5,9,2] packets”

Parameter Prioritization

All of the three parameter types above can be used within a single method definition however there is some type of conventional parameter type precedence. It is important to note that required parameters are the most favored, followed by default parameters then lastly optional parameters. Lets use a final method definition example that contains all of them.

def parameters (arg1,arg2=4, arg3*)“I’ll type #{arg1} then later type #{arg2} then finish with #{arg3}”end

The following method calling scenarios will help you better understand the precedence:

#method call with 3 arguments

parameters(1,2,3)===> “I’ll type 1 then later type 2 then finish with 3

#method call with 6 arguments

parameters(1,5,6,9,3,4)===>“I’ll type 1 then later type 5 then finish with [6,9,3,4]

#method call with 2 arguments

parameters(1,2)===>“I’ll type 1 then later type 2 then finish with []

#method call with 1 argument

parameters(6)===>“I’ll type 6 then later type 4 then finish with []

Anything you don't understand yet? Ask in the comment section below .. :-)

THE END

--

--