From: Robert Klemme Date: 2009-12-02T01:32:23+09:00 Subject: Re: Splat array with 1 value in Ruby 1.9 vs Ruby 1.8 2009/12/1 Raul Parolari : > Rick Denatale wrote: >> On Tue, Dec 1, 2009 at 12:53 AM, Raul Parolari >> wrote: >>> >>> Of course, this can be handled by generating code that checks the array >>> size. But I am curious on this behavior of splat when the array contains >>> one element (I read about the features of splat in 1.9, but they seem to >>> have nothing to do with this). Does anyone know if it is a bug or >>> intentional? >> >> >> Not sure exactly what you mean by generating the code, but could you use >> >> var1 = val1 >> and >> var1, var2, varn = val1, val2, valn >> >> >> >> -- >> Rick DeNatale >> >> Blog: http://talklikeaduck.denhaven2.com/ >> Twitter: http://twitter.com/RickDeNatale > > As I said at the beginning of the post, the code is unpacking binary > strings, which generates an array (else of course I would have not added > the array, just for the fun of having to remove it). For example: > >   x, y = "\x23\x00\x61".unpack('Cn')  # => [35, 97]; so: x = 35; y = 97 > > Now, when there is just one variable to extract, we get (as expected): > >   z="\x00\x61".unpack('n')   # z = [97] > > To get the scalar values in both cases, in 1.8.7 a splat operator in > front of the right value expression (placed by the code generator) does > the job; in 1.9.1, it does not. The solution is simple, but I was not > asking for "a solution". > > In other words, I was just pointing out this difference of behavior: > >  a = *[ 3 ]   # =>  a =3        with Ruby 1.8.7 > >  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 -- remember.guy do |as, often| as.you_can - without end http://blog.rubybestpractices.com/