Named Parameters in Ruby

Oluwafisayo Oluwatayo Feb 15, 2024
  1. Types of Parameters in Ruby
  2. Optional Parameters in Ruby
  3. Named Parameters in Ruby
  4. Conclusion
Named Parameters in Ruby

When we talk about parameters, we refer to functions that we can assign values; these values are then displayed in place of the parameters.

For instance, when we define a parameter fruit and assign the value, "apple" to the parameter, we will see "apple" displayed.

Types of Parameters in Ruby

There are mainly two types of parameters that are mostly applied in Ruby; we have optional parameters and named parameters.

We will look at examples of the application of these two parameters to understand their varying differences, but one difference that can be noticed at a glance is that the optional parameter is defined with the = sign while named parameters are defined with the : sign.

Optional Parameters in Ruby

Optional parameters are mostly one-dimensional because we can only reassign a value when we define more than one value inside the parameter.

Let us look at the example below, create a Ruby file, name it new.rb and input these codes:

def show_name_and_address(name = 'James', address = 'Madison')
  puts "#{name}, #{address}"
end

show_name_and_address

optional parameters

We will see "James, Madison" displayed in the result. Now let us attempt to change the values in the parameter.

Create another file, name it new2.rb and type in these codes:

def show_name_and_address(name = 'James', address = 'Madison')
  puts "#{name}, #{address}"
end

show_name_and_address('Bond')

optional parameters with only one change allowed

We will see the result displayed as "Bond, Madison"; the only parameter whose value changed is the first one, the name parameter. But what if we want to change the value of the address parameter?

Let us create a new file, name it new3.rb and input this snippet:

def show_name_and_address(name = 'James', address = 'Madison')
  puts "#{name}, #{address}"
end

show_name_and_address(address = 'Bond')

When we run this code, we will see that only the name parameter gets changed.

cannot specify with optional

The address parameter can’t be changed; this is where the named parameter works better compared to the optional parameter.

Named Parameters in Ruby

Unlike the optional parameters, the named parameters offer a much more flexible approach when adding values to functions. It allows us to change the values we assign to both the name and address parameters.

To see this in practice, let us look at the examples below.

Create a new file, name it new.rb and add these codes:

def show_name_and_address(name: 'James', address: 'Madison')
  puts "#{name}, #{address}"
end

show_name_and_address(name: 'Bond')

named parameter change name

We will see that the value assigned to the name variable is changed to "Bond". But this is alright; it’s what we can do with the optional parameter.

Now let us look at assigning a value to the address variable.

Create a new file, name it new2.rb and type in these codes:

def show_name_and_address(name: 'James', address: 'Madison')
  puts "#{name}, #{address}"
end

show_name_and_address(address: 'Bond')

named parameter change address

We will see that the value assigned to the address parameter changes to Bond.

If you want to see the named parameter display the same result as the first example, we did with the optional parameter, change the = sign to :.

Conclusion

The named parameter works better than the optional parameter when we need to change the values of more than one parameter. Applying either method will depend on what we want to achieve.

Oluwafisayo Oluwatayo avatar Oluwafisayo Oluwatayo avatar

Fisayo is a tech expert and enthusiast who loves to solve problems, seek new challenges and aim to spread the knowledge of what she has learned across the globe.

LinkedIn