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:
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.
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).
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:
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
Jamis gave me permission to send out a small mixin he wrote a couple
years back that takes care of all this for you. He never made a public
release of it; we were just playing with the idea at work one time and
he put it all together in a module to use later. I'd almost forgotten
about it.
Come Ruby 1.9/2.0, we'll have much of this built-in to the language.
I don't remember how well ruby-talk handles attachments, so forgive me
if this doesn't come through right the first time.