In computer programming, named parameters or keyword arguments refer to a computer language’s support for function calls that clearly state the name of each parameter within the function call itself. © Wikipedia
Right now (I mean in Ruby <= 1.9.3) you can ‘emulate’ it with passing a hash as an argument to a method:
1 2 3 4 5 |
|
I bet you saw this a lot. But the main problem with this technique is you can’t easily set default value for arguments. For sure you can solve it like this:
1 2 3 4 5 6 |
|
but it doesn’t look like a clean solution. So what will be changed with Ruby 2.0? Let’s look at this CHANGELOG entry:
Implement keyword arguments. The feature is promised to be included in 2.0, but the detail spec is still under discussion; this commit is a springboard for further discussion. Please try it and give us feedback.
Sounds promising! As you can see there is a chance that keyword arguments will be a part of ruby syntax. So how to use it? There is a couple of tests here. Let’s take a brief look at some of them:
- only keyword parameters
1 2 3 4 5 6 7 8 |
|
So as you can see you don’t need to do merge or something to achieve default values and if you pass an argument that doesn’t match arguments from the method’s definition you’ll get ArgumentError
. To make something similar Rails extends Hash
class with assert_valid_keys method.
- ordinary argument with keyword arguments
1 2 3 4 5 6 |
|
- keyword arguments and options
1 2 3 4 5 6 |
|
As you can see there is a new thing like **
. It allows you to pass a hash to this method because you can’t write
1 2 3 |
|
It won’t work. You’ll get something like syntax error, unexpected ')', expecting end-of-input
.
- it works with blocks too
1
|
|
So let’s keep fingers crossed for adding this feature to ruby 2.0.