Forum: Ruby I can't get hash list params working

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
A90204c955db033cd975f7bb0ec9600b?d=identicon&s=25 Ashley Moran (Guest)
on 2006-04-02 16:45
(Received via mailing list)
Hi

I must be doing something stoopid here.  Can anyone explain why I get
an error on the last line of this irb session?

irb(main):014:0> def a(a=nil,*b)
irb(main):015:1>   puts a.inspect
irb(main):016:1>   puts b.inspect
irb(main):017:1> end
=> nil
irb(main):018:0> a(1 => "one", 2 => "two")
{1=>"one", 2=>"two"}
[]
=> nil
irb(main):019:0> a(1 => "one", 2 => "two", 1)
SyntaxError: compile error
(irb):19: parse error, unexpected ')', expecting tASSOC
         from (irb):19
         from :0


Surely the output should be
   {1=>"one", 2=>"two"}
   [1]
?

I tried the example in Pickaxe v2 (p349) and got a similar error.
Someone please slap my head for me :)

thanks
Ashley
1fba4539b6cafe2e60a2916fa184fc2f?d=identicon&s=25 unknown (Guest)
on 2006-04-02 16:48
(Received via mailing list)
Hi --

On Sun, 2 Apr 2006, Ashley Moran wrote:

> irb(main):018:0> a(1 => "one", 2 => "two")
> Surely the output should be
> {1=>"one", 2=>"two"}
> [1]
> ?

You can only use the "magic" hash argument if it's the last thing in
the arglist.  If it isn't, you have to write it like a regular hash:

   a({1 => "one", 2 => "two"}, 1)


David

--
David A. Black (dblack@wobblini.net)
Ruby Power and Light, LLC (http://www.rubypowerandlight.com)

"Ruby for Rails" chapters now available
from Manning Early Access Program! http://www.manning.com/books/black
This topic is locked and can not be replied to.