cool. I like Gavin’s code below.
Is there a Ruby Tips/SampleCodes page?
Gavin’s code is worth pasting 
…probably under Tips->Argument Processing->Gavin->Sample code…
Kind regards,
-botp
···
Gavin Sinclair [mailto:gsinclair@soyabean.com.au] expounded:
…
Amidst all the discussion of various ways of achieving
keyword arguments or similar, one really nice feature has
been overlooked: easy default arguments. Here is a
representation of code I’ve written recently.
class Recorder
def initialize(init_params={})
init = process_params(init_params)
@database = init[:DATABASE]
@user = init[:USER]
@password = init[:PASSWORD]
end
def Recorder.init_defaults
{
:DATABASE = “…”,
:USER = “…”,
:PASSWORD = “…”,
}
end
def process_params(hash)
defaults = init_defaults()
# Guard user against invalid keys
hash.keys.each do |key|
raise “Key not supported: #{key}” unless defaults.has? key
end
defaults.update(hash)
end
end
Now,
Recorder.new and
Recorder.new(:USER => “smith”, :PASSWORD => “apple”)
will use the appropriate defaults, and
Recorder.new(:LOG_FILE => “x.log”)
will fail with a meaningful error message.
Further, if anyone wants to find out the defaults, they only
have to ask:
Recorder.init_defaults
I’ve been very happy with this, and see no need for Ruby to
adopt explicit keyword arguments.
Gavin
cool. I like Gavin’s code below.
Is there a Ruby Tips/SampleCodes page?
Gavin’s code is worth pasting 
…probably under Tips->Argument Processing->Gavin->Sample code…
Kind regards,
-botp
I was one step ahead of you and placed it at the link below 
http://www.rubygarden.org/ruby?KeywordArguments
If anyone makes a Wiki/other page where this would be relevant, they are free
to link to it (obviously) or steal it.
And I don’t know if there’s a Tips/SampleCodes page (well, I’m sure there’s
something), but if there isn’t, here’s what I recommend you do if you’re
interested:
- create one on the Wiki
- search the Wiki for all relevant tips etc.
- link to them from your new Wiki page
- for tips you think that would be good, code them yourself or
ask the list for suggestions
I’d certainly contribute whatever snippets I can.
Gavin
···
From: “Peña, Botp” botp@delmonte-phil.com
I was one step ahead of you and placed it at the link below 
http://www.rubygarden.org/ruby?KeywordArguments
I’ve heavily updated this page, most notably including specifying mandatory
arguments. That all you’ll get out of me on the subject of keyword arguments.
Of course, if anyone has any suggestions/bugs/comments, I’d be delighted to
hear them.
Gavin
···
From: “Gavin Sinclair” gsinclair@soyabean.com.au
And I don’t know if there’s a Tips/SampleCodes page (well, I’m sure there’s
something), but if there isn’t, here’s what I recommend you do if you’re
interested:
- create one on the Wiki
- search the Wiki for all relevant tips etc.
- link to them from your new Wiki page
- for tips you think that would be good, code them yourself or
ask the list for suggestions
I’d certainly contribute whatever snippets I can.
Gavin
I’ve created CategoryTips and CategorySampleCode so that appropriate pages can
be categorised. You all know how categories work, right? If not, see
http://www.rubygarden.org/ruby?CategoryCategory
Gavin
···
From: “Gavin Sinclair” gsinclair@soyabean.com.au