Methods with lots of arguments

D

Dido Sevilla

Just went through a bit of refactoring on a bit of Ruby code that I've
been writing, and have been following some of the guidelines here:

http://rpa-base.rubyforge.org/wiki/wiki.cgi?GoodAPIDesign

I've had a lot of methods in the old code that had lots and lots of
arguments, and many of which had default values, and followed the
suggestion on naming arguments by passing what is essentially a hash.
The syntax for this looked a lot like the labeled arguments used in
OCaml, and it seems like a good way of doing things. Now only if Ruby
had actual support for this idiom. As it turns out, I wind up writing
a lot of methods that look like this:

def somemethod(args)
requiredargs =3D [ :foo, :bar, :baz ]
defaultargs =3D { :quux =3D> nil, :blargle =3D> nil }
requiredargs.each {
|arg|
unless (args.has_key?(arg))
raise ArgumentError.new("missing required argument #{arg.to_s}")
end
}
newargs =3D defaultargs.merge(args)
...
end

Is there at the very least some kind of library out there that can do
all of this checking and merging automatically? If there is one, I
think it should be mentioned in the RPA-base link. However, I think
that this kind of idiom would be better as an official part of Ruby
syntax.
 
P

Peter C. Verhage

I would write this method like this:

def somemethod(foo, bar, baz, *options)
quux = options[:quux] || nil # or another default value
blargle = options[:blargle] || nil # or another default value
...
end

This method can be called as follows:
somemethod('a', 'b', 'c', :blargle => 'test')

But I don't this it's too hard to make a small helper class for this, to
make this even more easy, there probably already exists one (but I
wouldn't know).

Regards,

Peter
 
C

Carlos

Just went through a bit of refactoring on a bit of Ruby code that I've
been writing, and have been following some of the guidelines here:

http://rpa-base.rubyforge.org/wiki/wiki.cgi?GoodAPIDesign

I've had a lot of methods in the old code that had lots and lots of
arguments, and many of which had default values, and followed the
suggestion on naming arguments by passing what is essentially a hash.
The syntax for this looked a lot like the labeled arguments used in
OCaml, and it seems like a good way of doing things. Now only if Ruby
had actual support for this idiom. As it turns out, I wind up writing
a lot of methods that look like this:

def somemethod(args)
requiredargs = [ :foo, :bar, :baz ]
defaultargs = { :quux => nil, :blargle => nil }
requiredargs.each {
|arg|
unless (args.has_key?(arg))
raise ArgumentError.new("missing required argument #{arg.to_s}")
end
}
newargs = defaultargs.merge(args)
...
end

Is there at the very least some kind of library out there that can do
all of this checking and merging automatically? If there is one, I
think it should be mentioned in the RPA-base link. However, I think
that this kind of idiom would be better as an official part of Ruby
syntax.

Well, if you are repeating code, you should put it in a method...

class Hash
def setup_args! (desc)
desc[:required] ||= []
desc[:default] ||= {}
desc[:required].each do |arg|
unless self.has_key?(arg)
raise ArgumentError.new("missing req arg")
end
end
self.replace desc[:default].merge(self)
end
end

def somemethod (args)
args.setup_args! :)required => [:foo, :bar, :baz],
:default => { :quux => nil, :blarg => 77})
...
end

(not tested)

HTH
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,483
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top