On 18/10/05, Christophe Grandsire <christophe.grandsire / free.fr> wrote:
> En r?ponse ? Warren Seltzer :
> > I see this notation, which appears to be undefined outside of hash literals, all over the
> > place.   What does it do?  The online version of the pickaxe book doesn't have it.
> >
> > Here's a rails example:
> >
> > def do_something
> >     redirect_to :action => "elsewhere"
> >     render :action => "overthere" # raises DoubleRenderError
> > end
> >
>
> Basically, those are hash literals too. They look a bit like keyword
> arguments because of the syntactic sugar that Ruby allows:
> - If the only argument of a method is a hash, you can lose the {}:
> "mymethod({:foo => "bar"})" can be written "mymethod(:foo => "bar")"
> - Parentheses around the parameter list of a method are optional:
> "mymethod(foo)" can be written "mymethod foo"
> Add both rules, and you get exactly the syntax of your example.
> "redirect_to" and "render" are actually just methods that take hash
> arguments ('redirect_to :action => "elsewhere"' is just another way to
> write 'redirect_to({:action => "elsewhere"})' in a clearer and key-hit
> saving way ;) ), and the two syntactic sugar rules allows one to use
> them in a keyword+keyword argument fashion. Neat isn't it? :)
>
> As for why the online Pickaxe doesn't have it, it may be because the
> hash syntactic sugar is only present since Ruby 1.8, while the online
> Pickaxe covers Ruby 1.6. But I could be wrong.
> --
> Christophe Grandsire.
>
> http://rainbow.conlang.free.fr
>
> You need a straight mind to invent a twisted conlang.
>
>

It works not only when the method has only one argument, but always
with the last argument.

$ irb
irb(main):001:0> def show(*args) p args end
=> nil
irb(main):002:0> show 1, 2, 3, 5 => 6
[1, 2, 3, {5=>6}]
=> nil

regards,
brian

--
http://ruby.brian-schroeder.de/

Stringed instrument chords: http://chordlist.brian-schroeder.de/