Author: Yusuke Endoh Translator: Leonard Chin (@lchin)
Congratulations for the release of Ruby 2.0.0!
In this article, I will introduce one of the new features in Ruby 2.0.0, Keyword Arguments.
The following example implements a logging method.
It looks so ordinary. It doesn’t exactly look like anything new, does it?
We can already call methods with keyword arguments in 1.9.
The keyword arguments here are passed to the method as the hash { :level => “INFO” } To actually make use of the arguments, we need to access them through the hash.
So far, it isn’t all that complicated. However, things can get hairy real fast if we consider the following situations
To handle all the above cases, we end up with something like the following.
Not something you’d want to write over and over again. Handling these kinds of situations is the motivation behind keyword arguments. By using this new feature of Ruby 2.0.0, we can process keyword arguments extremely cleanly. Just like our opening example, in fact.
When we call this method without any keyword arguments, it behaves as we had passed it level: “ERROR”, time: Time.now
Order is not important of keyword arguments. However, you still need to pay attention to the order of the other arguments.
You can even leave out one of the keyword arguments.
If you supply an unknown keyword, an exception is raised.
Those who don’t want exceptions to be raised for unknown keywords can specify a hash argument with ** to explicitly group the other keyword arguments.
It is also possible to combine keyword arguments with optional arguments or variable-length argument lists. (But don’t go overboard!)
Be careful when passing hashes to methods with both variable length argument lists and keyword arguments. For example, in the case below, the last hash is interpreted as a keyword argument and won’t be assigned to the args parameter.
Also, you cannot omit the name of the ** parameter used to suppress unknown keyword exceptions.
(While writing this article, I started having doubts about this behaviour…)1
Care must also be taken when rewriting existing functions to take advantage of this feature. Unlike hash keys, keywords can not be reserved words in Ruby.
The above code won’t explicitly fail, but you won’t be able to access the contents of the local variable named if
. To get around this, you will need to use the ** parameter.
In Ruby 2.0.0, keyword arguments were made available by adding new syntax for defining method parameters.
The implementation of the feature is mostly syntax sugar, and can hardly be called a significant new feature. From the standpoint of the Ruby culture, however, I believe it is important that keyword arguments are now first class citizens. I think we will be seeing many new APIs taking advantage of keyword arguments, hopefully making your Ruby life just a little bit nicer.
Yusuke Endoh is a Ruby committer (account: mame). His contributions include increasing the test coverage of Ruby, serving as the assistant Release Manager of Ruby 1.9.2, and the Release Manager of Ruby 2.0.0.
This behaviour has been fixed. It will be included in a future patch level release of Ruby. https://bugs.ruby-lang.org/issues/7922 ↩