Exploring the Power of Keyword Arguments in Ruby
Tags: ruby, rails
Published: 2023-11-26 19:01:36


Keyword Arguments was introduced in Ruby 2.0. 

This seems a very nice and attractive feature. 

In a few words and code examples I will delve into **keyword arguments** in Ruby based on my own experience, opinion, and how I understand it.

This technique eliminates the **Dependency on Parameter Order** and **Provide More Flexibility for Default Values.** 



:traffic_light:
## Eliminating the Dependency on Parameter Order:
When using positional arguments, developers must know/remember the order in which the parameters are defined in the method definition. This can lead to confusion and errors, especially when methods have a large number of parameters. Keyword arguments eliminate this dependency by allowing developers to pass arguments by name instead of position. Let's consider an example:

### With positional(traditional) parameters:
```ruby
def greet(name, age)
  puts "Hello, #{name}! You are #{age} years old."
end

greet("Montells", 44)
> Hello, Montells! You are 44 years old.
greet(44, 'Montells')
> Hello, 44! You are Montells years old.

```
Here the developer must know the order of the parameters.
### With hash options:
```ruby
def greet(params)
  puts "Hello, #{params[:name]}! You are #{params[:age]} years old."
end

person = { name: 'Montells', age: '44' }
greet(person)
> Hello, Montells! You are 44 years old.
unordered_person = { age: '44', name: 'Montells' }
greet(unordered_person)
> Hello, Montells! You are 44 years old.
```
Here the dependency of order parameters is eliminated however developer needs to deal with hash.

### With Keyword Arguments:
```ruby
def greet(name:, age:)
  puts "Hello, #{name}! You are #{age} years old."
end

greet(name: "Montells", age: 44)
> Hello, Montells! You are 44 years old.
greet(age: 44, name: "Montells")
> Hello, Montells! You are 44 years old.
```
Here the developer must to know the name of each parameter but not the order.


## Ease of Providing Default Values:
Keyword arguments also provide a convenient way to assign default values to parameters, regardless of their order. This is particularly useful when some parameters have default values and others do not. Let's illustrate this with an example:

### With positional(traditional) parameters:
```ruby
def create_user(name, age = 18, country = 'Unknown')
  puts "Name: #{name}, Age: #{age}, Country: #{country}"
end

create_user("Montells", "Cuba") # I want to omit age
> Name: Montells, Age: Cuba, Country: Unknown
```


### With Keyword Arguments:
```ruby
def create_user(name:, age: 44, country: 'Unknown')
  puts "User: #{name}, Age: #{age}, Country: #{country}"
end

create_user(name: "Montells", country: 'Cuba')
> User: Montells, Age: 44, Country: Cuba
```

In the first example, using positional arguments, we need to remember the order of the default parameters. However, in the second example, with keyword arguments, we can explicitly provide values only for the parameters we want to override, while the rest use their default values. This makes the code more flexible and less error-prone.


### Bonus Track

Some similarity can be detected between `hash` and `keyword arguments`. Sometimes you have a hash options like `{name: "Montells", country: 'Cuba'}`. It can be passed to a method declared with keyword arguments using the double splat operator `**`

```ruby
def create_user(name:, age: 44, country: 'Unknown')
  puts "User: #{name}, Age: #{age}, Country: #{country}"
end

hash_options = {name: "Montells", country: 'Cuba'}

create_user(**hash_options)
>User: Montells, Age: 44, Country: Cuba
```

:checkered_flag:

Conclusion:
Keyword arguments in Ruby, introduced in version 2.0, offer significant advantages over traditional positional arguments. They eliminate the need to remember the order of parameters, making code more readable and self-explanatory. Additionally, keyword arguments provide a convenient way to assign default values to parameters, regardless of their order. By leveraging the power of keyword arguments, Ruby developers can write more expressive, flexible, and maintainable code.

Incorporating keyword arguments into your Ruby code can greatly enhance its readability and maintainability. Embrace this powerful feature and unlock the full potential of your Ruby programming skills. Happy coding!