Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Ruby > ruby 1.9 splat in return statement, bug or feature?

Reply
Thread Tools

ruby 1.9 splat in return statement, bug or feature?

 
 
Gary Wright
Guest
Posts: n/a
 
      02-17-2007
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




 
Reply With Quote
 
 
 
 
Pea, Botp
Guest
Posts: n/a
 
      02-17-2007
From: Gary Wright [(E-Mail Removed)]=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



 
Reply With Quote
 
 
 
 
Charles Lowe
Guest
Posts: n/a
 
      02-17-2007
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/.

 
Reply With Quote
 
Macario Ortega
Guest
Posts: n/a
 
      06-03-2009

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 <(E-Mail Removed)>
> 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/.

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
what's up with return *splat ? Phlip Ruby 8 06-24-2012 11:27 PM
Splat array with 1 value in Ruby 1.9 vs Ruby 1.8 Raul Parolari Ruby 15 12-21-2009 08:18 AM
redefining splat? Adam Shelly Ruby 10 10-03-2006 03:51 PM
Splat, #to_ary and #to_a Eero Saynatkari Ruby 18 09-20-2006 05:22 PM
Doing a splat within a C extension Daniel Berger Ruby 3 11-03-2005 01:33 PM



Advertisments