From: Adam Prescott Date: 2011-01-18T06:17:37+09:00 Subject: Re: Parallel Assignments and Elegance/Complexity Ratio. --90e6ba615092826da1049a114b89 Content-Type: text/plain; charset=UTF-8 > > > def parallel(values) > > x, (y, (z, a))=values > > [x,y,z,a] > > end > > Instead you could also do: > > x, y, z, a = values.flatten > > Which avoids making you look like your writing in LISP. These two bits of code are not the same thing. a, (b, c) = [1, [2, [3, 4]]] a #=> 1 b #=> 2 c #=> [3, 4] a, b, c = [1, [2, [3, 4]]].flatten a #=> 1 b #=> 2 c #=> 3 On Sat, Jan 15, 2011 at 11:04 AM, Joseph Lenton wrote: > Josh Cheek wrote in post #974019: > > On Tue, Jan 11, 2011 at 8:29 AM, Kedar Mhaswade > > wrote: > > > >> > >> Kedar > >> > >> -- > >> Posted via http://www.ruby-forum.com/. > >> > >> > > I don't use it very often, but when I do, it usually makes an elegant > > solution. I think part of the reason it doesn't seem that way is because > > you > > are playing with it in too sterile of an environment. For example, you > > rate > > "x, (y, (z, a))=[1, [2, [3, 4]]]" as lowest, suggesting it is equivalent > > to > > "x=1;y=2;z=3;a=4" but this is not true. If you are actually assigning > > with > > literals, you would, of course, use the equivalent way, but if your data > > comes in as nested arrays, then you can't assign like that, instead you > > have > > to do something like this: > > > > def parallel(values) > > x, (y, (z, a))=values > > [x,y,z,a] > > end > > Instead you could also do: > > x, y, z, a = values.flatten > > Which avoids making you look like your writing in LISP. > > For small stuff parrallel assignment makes it look elegant, but like any > language feature it can be abused. Although I don't think I've ever > actually found a case where I've wanted to use this. Especially when > most of the time my 'x, y, z and a' variables are unrelated so I don't > want to use them together in the same statement. > > -- > Posted via http://www.ruby-forum.com/. > > --90e6ba615092826da1049a114b89--