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?

    r@pc4all:~# ruby -e 'def foo; return *[1]; end; p foo'
    1
    r@pc4all:~# 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

    Peña, Botp wrote:
    > ...
    >
    > consistency perhaps?
    >
    > r@pc4all:~# ruby -e 'def foo; return *[1]; end; p foo'
    > 1
    > r@pc4all:~# ruby -e 'def foo; return *[1,2]; end; p foo'
    > [1, 2]
    >
    > kind regards -botp


    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.

    --
    Posted via http://www.ruby-forum.com/.
     
    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.




    Yukihiro Matsumoto wrote:
    > Hi,
    >
    > In message "Re: ruby 1.9 splat in return statement, bug or feature?"
    > on Sat, 17 Feb 2007 12:36:20 +0900, Gary Wright <>
    > writes:
    >
    > |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]
    >
    > In 1.9, values (i.e. result of splat) are always represented by array,
    > so that we won't confuse array as an value with array as values
    > representation.
    >
    > matz.


    --
    Posted via http://www.ruby-forum.com/.
     
    Macario Ortega, Jun 3, 2009
    #4
    1. Advertisements

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. Seong-Kook Shin
    Replies:
    1
    Views:
    690
    Richard Bos
    Jun 18, 2004
  2. Greenhorn
    Replies:
    15
    Views:
    1,150
    Keith Thompson
    Mar 6, 2005
  3. Gour
    Replies:
    1
    Views:
    216
    WATANABE Hirofumi
    Jul 29, 2003
  4. Daniel Berger

    Doing a splat within a C extension

    Daniel Berger, Nov 3, 2005, in forum: Ruby
    Replies:
    3
    Views:
    225
  5. Eero Saynatkari

    Splat, #to_ary and #to_a

    Eero Saynatkari, Sep 18, 2006, in forum: Ruby
    Replies:
    18
    Views:
    404
    Rick DeNatale
    Sep 20, 2006
  6. Raul Parolari
    Replies:
    15
    Views:
    316
    Robert Klemme
    Dec 21, 2009
  7. robic0
    Replies:
    7
    Views:
    247
    Samwyse
    Jan 24, 2006
  8. Phlip
    Replies:
    8
    Views:
    928
    Phlip
    Jun 25, 2012
Loading...