Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Ruby > I can't get hash list params working

Reply
Thread Tools

I can't get hash list params working

 
 
Ashley Moran
Guest
Posts: n/a
 
      04-02-2006
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


 
Reply With Quote
 
 
 
 
dblack@wobblini.net
Guest
Posts: n/a
 
      04-02-2006
Hi --

On Sun, 2 Apr 2006, Ashley Moran wrote:

> 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]
> ?


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 ((E-Mail Removed))
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


 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
hash of hash of hash of hash in c++ rp C++ 1 11-10-2011 04:45 PM
ruby-odbc and OUTPUT params not working Chris Hall Ruby 0 03-20-2009 05:50 PM
use hash to name params, then stringify values in specific order DJ Stunks Perl Misc 3 01-21-2009 12:38 AM
Hash#select returns an array but Hash#reject returns a hash... Srijayanth Sridhar Ruby 19 07-02-2008 12:49 PM
params v.s. @params in rails? Barry Ruby 9 09-15-2005 03:12 AM



Advertisments