Hi --

On Wed, 12 Mar 2008, Max Williams wrote:

> I'm using the method of parameter passing where i have one 'regular'
> parameter and then a hash of options:
>
> def chunkify(something, options = {})
>  ...
> end
>
> I want to have it so that the user can just pass a key through without
> having to set it to anything, eg
>
> dinner = chunkify(bacon, :extra_fine)
>
> However, 'extra_fine' is just being treated as a symbol, rather than a
> hash key, and so 'options' is simply the symbol :extra_fine, rather than
> the hash {:extra_fine => nil}.
>
> I can fix this by passing :extra_fine => true to the method, but can i
> set up the method so it will just take the hash key, and maybe some
> other hash_keys, some with and some without values?

You're over-thinking this a bit. :extra_fine isn't any more a hash key
than 100 is in this:

    do_something(100)

The only thing that tells Ruby that you want a hash as the last
argument, in the absence of curly braces, is the hash separator (=>).

    do_something(100 => "C")

There's also no such thing as a hash key that isn't part of a hash. It
sounds like what you want to do is pass multiple objects to your
method and create a hash from them inside the method.


David

-- 
Upcoming Rails training from David A. Black and Ruby Power and Light:
   ADVANCING WITH RAILS, April 14-17 2008, New York City
   CORE RAILS, June 24-27 2008, London (Skills Matter)
See http://www.rubypal.com for details. Berlin dates coming soon!