Ruby Keyword Arguments

Whenever new versions Ruby, Rails, or Android comes out, I quickly glance over the change logs and go, “Oh, neat! That’s a cool feature, I’m going to start using that!”

But, habits are hard to break. In my prior freelance gigs and jobs, whenever I wanted to have default values for parameters in a method, I would always do it this way.

If we wanted the method foo to accept multiple parameters and we wanted them to all have default parameters, we would do this.

And so forth. Apparently in Ruby 2.0, which has been out for awhile (yes, I am slow), something called Keyword Arguments was introduced. So, instead of doing the above where you have to worry about ordering of your parameters, you can simply do

This is essentially the same thing as passing in a hash as a parameter and then setting default values for those parameters before writing the actual meat of your method. In Ruby 1.9, the above foo method would have been written as

I think the keyword arguments version is prettier. After looking more deeply into this, it seemed that in Ruby 2.0, we couldn’t make keyword arguments required. However, Ruby 2.1 introduced keyword arguments. To make keyword arguments required, you simply omit the default value after the key, like this.

Keyword arguments vs positional arguments

Personally, I prefer keyword arguments, because it makes reading code much easier. For example, let’s say that you have a method that calculates profitability of a business. Let’s keep it simple and say that the formula is simply revenue - cost.

The first way will be with positional arguments

If someone was looking at the line where we’re calling the method profit with the parameters 500 and 200, that person may wonder, “Okay, what does 500 and 20 represent?”. He/she would then have to locate where the method profit is and read the method to find out that 500 is the revenue and 100 is the cost. Let’s look at what keyword argument version looks like.

In the Keyword Argument version, it’s clear that 500 is the revenue and 50 is the cost as it’s literally written in the line where we’re calling the method profit. I personally find the keyword argument version much more clear.

Some gotchas

I think most people would agree that keyword arguments is more clear than positional arguments. However, using positional arguments is a more succinct way to write a method, so you’ll have to decide between the two and pick the one that makes sense for the method that you are writing. Personally, if the method only takes one arguments or if the arguments that the method takes in is clear/obvious, I think positional arguments make sense. In all other cases, I prefer keyword arguments.

Also, keyword arguments are essentially a hash disguised as a parameter. Sandi Metz once said in a talk she gave (and I believe in her book Practical Object Oriented Design in Ruby) that a method should take in no more than 4 parameters. If it does, there’s probably another method or an object in there.

This “rule” shouldn’t be broken by passing in one hash with bunch of key/value pairs, and while keyword arguments wouldn’t tempt developers due to its syntactical nature of it looking like actual parameters rather than one single hash, it can still be abused if one wants to take advantage of the fact that ordering of parameters is not important in keyword arguments.

For those of you who weren’t aware of its existence, enjoy writing more clearly defined methods with keyword arguments.