From: hemant Date: 2007-02-16T01:23:02+09:00 Subject: Re: is it behaving strange ? On 2/15/07, sur max wrote: > ok and its like .... > > *a = 9 remember a would become an array here.because as explained above,if lvalue is prefixed with asterisk, then rvalue is collected and assigned to lvalue as an array. > a << 5 append 5 to a (which is an array) > b = *a # => [9,5] > is there any relation in this n above ? Now, above is pretty standard ruby behaviour, it has nothing to do with * operator, as usual * extracts the values from a(which was an array). For example: irb(main):062:0> b = 9,5 [9, 5] irb(main):063:0> b [9, 5] irb(main):064:0> b,c = 9,5 [9, 5] irb(main):065:0> b 9 irb(main):066:0> c 5 > > On 2/15/07, dblack@wobblini.net wrote: > > > > Hi -- > > > > On Fri, 16 Feb 2007, hemant wrote: > > > > > On 2/15/07, sur max wrote: > > >> *a = 9 # => [9] > > >> a # => [9] > > >> *a # => compile error > > >> *b=*a # => [9] > > >> *b = a # => [[9]] > > >> b = a # => [9] > > >> b = *a # => 9 ----- this is amazing ? > > > > > > Splat operator is amazing, but above behaviour can be explained using > > this: > > > > > > "If the last lvalue is preceded by an asterisk, all the remaining > > > rvalues will be collected and > > > assigned to that lvalue as an array. Similarly, if the last rvalue is > > > an array, you can > > > prefix it with an asterisk, which effectively expands it into its > > > constituent values in > > > place. (This is not necessary if the rvalue is the only thing on the > > > right side�the array > > > will be expanded automatically.) > > > " > > > See, nothing wierd out there. ;) > > > > Another way to look at it, which I think covers all of the above > > cases, is: > > > > *x equals [x] without the [] > > > > So: > > > > *a = 9 # a without the [] is 9, so a == [9] > > *b = a # b without the [] is [9], so b == [[9]] > > b = *a # b == [9] without the [], so b == 9 > > > > > > That's why I call it the "unary unarray" operator. > > > > > > David > > > > -- > > Q. What is THE Ruby book for Rails developers? > > A. RUBY FOR RAILS by David A. Black (http://www.manning.com/black) > > (See what readers are saying! http://www.rubypal.com/r4rrevs.pdf) > > Q. Where can I get Ruby/Rails on-site training, consulting, coaching? > > A. Ruby Power and Light, LLC (http://www.rubypal.com) > > > > > > -- > sur > http://expressica.com > -- gnufied ----------- There was only one Road; that it was like a great river: its springs were at every doorstep, and every path was its tributary. http://people.inxsasia.com/~hemant