I can't get hash list params working

Discussion in 'Ruby' started by Ashley Moran, Apr 2, 2006.

  1. Ashley Moran

    Ashley Moran Guest

    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
     
    Ashley Moran, Apr 2, 2006
    #1
    1. Advertising

  2. Ashley Moran

    Guest

    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 ()
    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
     
    , Apr 2, 2006
    #2
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. rp
    Replies:
    1
    Views:
    584
    red floyd
    Nov 10, 2011
  2. Barry
    Replies:
    9
    Views:
    485
    Ara.T.Howard
    Sep 15, 2005
  3. Srijayanth Sridhar
    Replies:
    19
    Views:
    670
    David A. Black
    Jul 2, 2008
  4. David Sainte-claire
    Replies:
    1
    Views:
    114
    yermej
    Nov 23, 2009
  5. DJ Stunks
    Replies:
    3
    Views:
    154
    DJ Stunks
    Jan 21, 2009
Loading...

Share This Page