Forum: Ruby ruby 1.9 splat in return statement, bug or feature?

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.
E7559e558ececa67c40f452483b9ac8c?d=identicon&s=25 Gary Wright (Guest)
on 2007-02-17 04:37
(Received via mailing list)
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
6087a044557d6b59ab52e7dd20f94da8?d=identicon&s=25 Peña, Botp (Guest)
on 2007-02-17 05:03
(Received via mailing list)
From: Gary Wright [mailto:gwtmp01@mac.com]
# 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
B55aa8835b13b2cb20220aa357ede4b1?d=identicon&s=25 Charles L. (aquasync)
on 2007-02-17 05:58
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.
0ec4920185b657a03edf01fff96b4e9b?d=identicon&s=25 Yukihiro Matsumoto (Guest)
on 2007-02-17 11:17
(Received via mailing list)
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 <gwtmp01@mac.com>
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.
7bf65dcb7e862416161ea9bb591c2d8d?d=identicon&s=25 Macario Ortega (macario)
on 2009-06-03 08:30
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 <gwtmp01@mac.com>
> 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.
This topic is locked and can not be replied to.