From: RichardOnRails Date: 2010-12-29T13:35:17+09:00 Subject: Re: What am I missing about splat operator? On Dec 28, 10:54 pm, RichardOnRails wrote: > On Dec 28, 10:12 pm, Ryan Davis wrote: > > > > > On Dec 28, 2010, at 18:50 , RichardOnRails wrote: > > > > In trying to understand the splat operator, I visited: > > >http://theplana.wordpress.com/2007/03/03/ruby-idioms-the-splat-operator/ > > > > The first example that site offers is: > > >   The split mode : > > >   pet1, pet2, pet3 = *["duck","dog","cat"] > > > > That resulted in pet1 == "duck", etc > > > > But so did: > > >   pet1, pet2, pet3 = ["duck","dog","cat"] # no splat operator > > > and# > > >   pet1, pet2, pet3 = "duck","dog","cat" # no array-literal markers > > > > So this first example makes no sense, does it?  What am I missing? > > > The fact that you're splatting an array doesn't matter, just the fact that you're splatting. > > > # 1 > > pet1, pet2, pet3 = *["duck","dog","cat"] > > > # 2 > > pet1, pet2, pet3 = ["duck","dog","cat"] # no splat operator > > > # 3 > > pet1, pet2, pet3 = "duck","dog","cat" # no array-literal markers > > > # parses as: > > # > > # s(:block, > > #   # 1 > > #   s(:masgn, > > #     s(:array, s(:lasgn, :pet1), s(:lasgn, :pet2), s(:lasgn, :pet3)), > > #     s(:splat, s(:array, s(:str, "duck"), s(:str, "dog"), s(:str, "cat")))), > > #   # 2 > > #   s(:masgn, > > #     s(:array, s(:lasgn, :pet1), s(:lasgn, :pet2), s(:lasgn, :pet3)), > > #     s(:to_ary, s(:array, s(:str, "duck"), s(:str, "dog"), s(:str, "cat")))), > > #   # 3 > > #   s(:masgn, > > #     s(:array, s(:lasgn, :pet1), s(:lasgn, :pet2), s(:lasgn, :pet3)), > > #     s(:array, s(:str, "duck"), s(:str, "dog"), s(:str, "cat")))) > > > # In all 3 cases, you're dealing with a multi-assign w/ an array on LHS. > > # > > # RHS: > > # 1) splatted array > > # 2) to_ary array (or any object, see below) > > # 3) array (whether you use [] or not, it is still an array literal) > > > a, b, c = x > > a, b, c = *x > > > # s(:block, > > #   # 1 > > #   s(:masgn, > > #     s(:array, s(:lasgn, :a), s(:lasgn, :b), s(:lasgn, :c)), > > #     s(:to_ary, s(:call, nil, :x, s(:arglist)))), > > #   # 2 > > #   s(:masgn, > > #     s(:array, s(:lasgn, :a), s(:lasgn, :b), s(:lasgn, :c)), > > #     s(:splat, s(:call, nil, :x, s(:arglist))))) > > Hi Ryan, > > It looks to me the you gave me Lisp expressions one could use, in > part, to translate the three right-hand-sides I provided.  I knew that > Ruby treats "x,y = 1,2" as equivalent to "x=1; y=2", > but I didn't know that Ruby first view them as "LHS-array assigned > values from a RHS-array" (which is how I interpret your expressions, > which look like Lisp to me). > > Ultimately, then, the website's first lines show me that splat on > arrays is an Identity prefix operator, without mentioning that > explicitly.  That's not very helpful, IMHO.  Don't you agree? > > Best wishes, > Richard A little more info ... this time, incorrect IMHO: Splat array of arrays: a = [[:planes, 21], [:cars, 36]] h = Hash[*a] # => {[:planes, 21]=>[:cars, 36]} Allegedly, h = Hash[*a] # => { :planes=>21, :cars=>36} Do you concur in my finding that the site's in error here? So, I hunt for a more informative (and more accurate) presentation of the virtues of splat. Again, Best Wishes, Richard