Ruby 1.9 splat in return statement, bug or feature?


#1

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 W.


#2

From: Gary W. [mailto:removed_email_address@domain.invalid]

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


#3

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.


#4

Hi,

In message “Re: ruby 1.9 splat in return statement, bug or feature?”
on Sat, 17 Feb 2007 12:36:20 +0900, Gary W. removed_email_address@domain.invalid
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.

#5

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 M. 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 W. removed_email_address@domain.invalid
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.