NamedParameters
is a library for (MRI) ruby 1.8.x that allows you to call a method
with or without named parameters.
Introduction
One thing that is notably lacking from Ruby is the ability to
arbitrarily call methods with named parameters.
Ex in this code:
class A
def method_with_lots_of_parameters param1, p2, p3, p4 = 5, p6 = 7, p7
= 8, p9 = 900
p7
end
end
A.new.method_with_lots_of_parameters 1, 2, 3, 4, 5, 6, 7
can anybody guess what p7 will be when you run this? (a contrived
example, but an example nonetheless).
Previous attempts to overcome this have dealt with using a hash for the
ending argument, and/or *args for parameters. These are somewhat tedious
and also a little error prone, as misspellations aren’t immediately
caught at run-time in the hash example, and parsing is annoying in the
*args example.
Enter named parameters.
With named parameters, you can define your method as you normally would,
and add one line:
class A
def method_with_lots_of_parameters param1, p2, p3, p4 = 5, p6 = 7, p7
= 8, p9 = 900
p7
end
and add this line
create_named_parameters_wrapper :method_with_lots_of_parameters
end
and now you can call it like
A.new.method_with_lots_of_parameters 1, 2, 3, 4, :p7 => 6
And it works as you’d expect.
How it works
It works by creating (in our example above) code equivalent to:
class A
save away the original function
alias :_method_with_lots_of_parameters :method_with_lots_of_parameters
def method_with_lots_of_parameters *args
param1, p2, p3, p4, p6, p7, p9 = args.interpret [:param1, :p2,
:p3], :p4, :p6, :p7, :p9 # parses what you passed it, using the last
hash for named parameters
if p4 == :__not_assigned
p4 = 5
end
if p6 == :__not_assigned
p6 = 7
if p9 == :__not_assigned
p9 = 900
end
_method_with_lots_of_parameters param1, p2, p3, p4, p6, p7, p9 #
call the original function
end
end
Note also that class methods need to be called preceded by ‘self.’, like
class A
def A.class_method1
end
create_named_parameters_wrapper :‘self.class_method1’
end
[to satisfy parseTree’s semantics.]
Also note that you must continue naming parameters once you start naming
them.
[i.e. method_with_lots_of_parameters :param1 => 2, 2, 3 is not allowed],
to help disambiguate which parameter is which [idea stolen from Python].
Many thanks to the parse tree and ruby2ruby crew for making this
possible.
In my own use of it I have seen increased code readability and
understandability, especially for methods that take more than a few
parameters.
Note also that if your current method takes an ending hash then this
won’t work, as it uses the ending hash for named parameters.
How to install
sudo gem install ruby2ruby # pre requisite
svn co
http://ruby-roger-useful-functions.googlecode.com/svn/trunk/arg_parser/v2
named_args
require ‘named_args/create_named_parameters_wrapper’
… # code as usual, adding create_named_parameters_wrapper calls to
functions you’d like to be able to have this functionality.
Feedback welcome.
This is just an early release as I wanted to publish in case it were
useful to anyone.
Take care.
=R