From: Robert Klemme Date: 2009-12-02T02:25:06+09:00 Subject: Re: Splat array with 1 value in Ruby 1.9 vs Ruby 1.8 On 12/01/2009 05:56 PM, Raul Parolari wrote: > Robert Klemme wrote: >> 2009/12/1 Raul Parolari : >>>> >>> >>> ?a = *[ 3 ] ? # => a = [ 3 ] ? ?with Ruby 1.9.1 >>> >>> I think that this is a bug in Ruby 1.9. >> Regardless whether it is or is not a bug (I would not be so sure of >> that), there is a way to fix it and handle it uniformly across Ruby >> versions: >> >> 17:31:18 test$ allruby -e 'a = [123];b = *[456];c, = [789]; p a,b,c' >> CYGWIN_NT-5.1 padrklemme1 1.5.25(0.156/4/2) 2008-06-12 19:34 i686 Cygwin >> ======================================== >> ruby 1.8.7 (2008-08-11 patchlevel 72) [i386-cygwin] >> [123] >> 456 >> 789 >> ======================================== >> ruby 1.9.1p129 (2009-05-12 revision 23412) [i386-cygwin] >> [123] >> [456] >> 789 >> ======================================== >> jruby 1.4.0 (ruby 1.8.7 patchlevel 174) (2009-11-02 69fbfa3) (Java >> HotSpot(TM) Client VM 1.6.0_17) [x86-java] >> [123] >> 456 >> 789 >> >> Look at variable "c". >> >> Kind regards >> >> robert > > Indeed, this solves with grace the problem in the automation (without > the easy but disturbing question on "how many elements the array has"): > > a,b, = [ 3, 4 ] # => a = 3; b = 4 > a, = [ 3 ] # => a = 3 > > On the other matter ( a = *[ 3 ] ), I still think that it is a bug in > 1.9 (else I don't understand fully what a splat operator is, and I'd > like to know more). I know there have been some things changed with regard to how the splat operator works, including more complex patterns: robert@fussel:~$ ruby1.9 -e 'def f(a,*b,c)p a,b,c end;f(1, 2, 3, 4)' 1 [2, 3] 4 This did not work in 1.8: robert@fussel:~$ ruby1.8 -e 'def f(a,*b,c)p a,b,c end;f(1, 2, 3, 4)' -e:1: syntax error, unexpected tIDENTIFIER, expecting tAMPER or '&' def f(a,*b,c)p a,b,c end;f(1, 2, 3, 4) ^ robert@fussel:~$ The changes may make it necessary that 1.9 behaves the way you observed. That's why I said I am not sure whether it is actually a bug. > But thanks a lot, Robert You're welcome! Kind regards robert -- remember.guy do |as, often| as.you_can - without end http://blog.rubybestpractices.com/