From: "David A. Black" Date: 2010-09-10T23:31:32+09:00 Subject: Re: Receiving array naturally? Hi -- On Fri, 10 Sep 2010, Clifford Heath wrote: > Terry Michaels wrote: >> If I might ask, what exactly is the line >> strings = *strings >> doing? >> For that matter, I could use a little more clarification on what the '*' >> (glob?) operator does... > > It's not really a normal operator, rather it's part of Ruby's syntax > that happens to look like an operator, and in some cases operates like > one. Wait... can you operate but not be an operator? :-) > Just a final note on multi-assignment, if there is a comma-separated > list on the left, possibly ending with a comma, only the respective > values are assigned and the rest are discarded. So these are equivalent: > > a, = *strings > a = strings[0] > > and so are these: > > a, b = *strings > a, b = strings[0], strings[1] > > The final thing you might want to do is: > > a, b, *c = *strings You don't need the * on strings in any of those, though: >> strings = %w{ one two three } => ["one", "two", "three"] >> a, = *strings; a => "one" >> a, = strings; a => "one" >> a,b = *strings; [a,b] => ["one", "two"] >> a,b = strings; [a,b] => ["one", "two"] >> a,b,*c = *strings; [a,b,c] => ["one", "two", ["three"]] >> a,b,*c = strings; [a,b,c] => ["one", "two", ["three"]] Those all work the same in 1.8. and 1.9, though 1.8 and 1.9 do some parallel assignment things somewhat differently from each other. Here's 1.8: >> *a = [1,2,3]; a => [[1, 2, 3]] and 1.9: >> *a = [1,2,3]; a => [1, 2, 3] David -- David A. Black, Senior Developer, Cyrus Innovation Inc. The Ruby training with Black/Brown/McAnally Compleat Philadelphia, PA, October 1-2, 2010 Rubyist http://www.compleatrubyist.com