From: dblack@... Date: 2006-08-13T08:39:22+09:00 Subject: Re: Newbie at ruby - syntactic sugar for Range / Array Hi -- On Sun, 13 Aug 2006, Rick DeNatale wrote: > On 8/12/06, dblack@wobblini.net wrote: > >> So *0..3 is almost like saying: you're a range, but pretend that >> you're a thing that can be un-arrayed, namely an array. That seems to >> be the purpose of the * in this case -- to "trick" the range into >> thinking it's an array. >> >> If you look at ranges as array-like lists of values in the first place >> (which I don't), then it might make sense in a less convoluted way :-) > > Or you can think of the * before the last rvalue in an assignment as a > signal to replace the rvalue with a series of rvalues obtained from > the elements of the array resulting from sending to_ary to the > original rvalue. > > The original rvalue doesn't need to be an array, or a range, just > anything which responds to to_ary > > And actually in Ruby 1.8, it looks like it can be any object, since it > seems to actually use to_a instead of to_ary which is defined in > Object to return an array containing the receiver. But this is > supposed to change in Ruby 1.9 Yes, that's a more cogent explanation, and it accounts for: a, b = "abc\ndef" => a == "abc\n", b = "def" So the only weird thing is that ranges can turn themselves into arrays :-) David -- http://www.rubypowerandlight.com => Ruby/Rails training & consultancy ----> SEE SPECIAL DEAL FOR RUBY/RAILS USERS GROUPS! <----- http://dablog.rubypal.com => D[avid ]A[. ]B[lack's][ Web]log http://www.manning.com/black => book, Ruby for Rails http://www.rubycentral.org => Ruby Central, Inc.