ruby 1.9 splat in return statement, bug or feature?

Discussion in 'Ruby' started by Gary Wright, Feb 17, 2007.

  1. Gary Wright

    Gary Wright Guest

    What's going on with splat in 1.9?


    $ ruby -e 'def foo; return *[1]; end; p foo'
    1

    $ ruby-1.9 -e 'def foo; return *[1]; end; p foo'
    [1]


    Gary Wright
     
    Gary Wright, Feb 17, 2007
    #1
    1. Advertisements

  2. Gary Wright

    Peña, Botp Guest

    From: Gary Wright [mailto:]=20
    # What's going on with splat in 1.9?
    # $ ruby -e 'def foo; return *[1]; end; p foo'
    # 1
    # $ ruby-1.9 -e 'def foo; return *[1]; end; p foo'
    # [1]

    consistency perhaps?

    [email protected]:~# ruby -e 'def foo; return *[1]; end; p foo'
    1
    [email protected]:~# ruby -e 'def foo; return *[1,2]; end; p foo'
    [1, 2]

    kind regards -botp
     
    Peña, Botp, Feb 17, 2007
    #2
    1. Advertisements

  3. Gary Wright

    Charles Lowe Guest

    It seems somehow less consistent when viewed another way though. I think
    splatting a literal array should be equivalent to having written a plain
    comma-delimited list:

    def bar(*args); end

    bar 1
    bar(*[1])
    bar 1, 2
    bar(*[1, 2])

    In which case, for return:

    return *[1] => return 1 => 1
    return *[1, 2] => return 1, 2 => [1, 2]

    Its not going to really bother me at any rate though.
     
    Charles Lowe, Feb 17, 2007
    #3
  4. Men thats a real bummer, i love "peeling" arrays like this:

    a = *[1] #=> 1
    a = *[1,2] # => [1,2]

    Some of my code is gonna break.




     
    Macario Ortega, Jun 3, 2009
    #4
    1. Advertisements

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments (here). After that, you can post your question and our members will help you out.