Ruby Methods Optional Default Arguments Tutorial

In this section, we will learn what default arguments are and how to use them in Ruby.

Note: we’re assuming you’re already familiar with the Ruby Methods.

What is Optional Default Arguments in Ruby?

So far, we know that when a method has a set of parameters; we need to fill those parameters with arguments. If we don’t do so, we will get an error!

But if you think about it, there can be methods with parameters that most of the times we know what value a user might set for those parameters!

Now in situations like this, in order to take the burden of the users from entering arguments for such type of parameters, we can use default arguments in Ruby.

The default arguments are a way of setting values for parameters of a method right when declaring those parameters in a method.

Now, when a parameter takes a default argument, that parameter becomes optional and that means we can skip the value for that parameter when calling the method.

Remember that if we skip passing a value to a parameter that has the default value, it will use the default value instead.

Alright, enough with theory and now let’s get into the syntax of using default arguments in methods.

Ruby Optional Default Argument Syntax:

def function_name (parameter1 = “Value”, parameter2 = value)

def function_name(parameter1, parameter2 = value)

Remember that when using default argument for a parameter, it is not necessary to set all the parameters of a method with a default value! Only those parameters that we actually want to set a default value for them and we can leave the others without any default arguments.

Note: if there’s a method with multiple parameters and only one or two parameters of that method have the default value, we should put those parameters as the last parameters of the method and bring those non-default parameters to the beginning of the method. This way your program can differentiate and realize what parameter should take the input argument when the method is getting called.

Example: creating methods with default arguments in Ruby

def employee_details(first_name ,last_name, company = "Apple")

    puts "The name is: #{first_name} and the last name is: #{last_name} and the person is working at: #{company}"

end 


employee_details("John","Doe")

Output:

The name is: John and the last name is: Doe and the person is working at: Apple

How does optional default argument work in Ruby?

In this example, the `employee_details` method has 3 parameters. For this particular example, our assumption is that the users who are going to use the method are working for the Apple company.

For this reason, we’ve set a default value for the third parameter of the method `the company parameter` and set it to “Apple”. That means now users are only needed to set the first and second parameters (the first_name and the last_name) and can skip over the value of the company parameter.

So in that case the default value of the `company` will be used in the body of the method when we call the method with just two parameters.

Facebook
Twitter
Pinterest
LinkedIn

Top Technologies