From: Josh Cheek Date: 2010-12-29T22:21:49+09:00 Subject: Re: What am I missing about splat operator? --0016e68db81c84b4a704988c73f2 Content-Type: text/plain; charset=ISO-8859-1 On Tue, Dec 28, 2010 at 9:12 PM, Ryan Davis wrote: > > # 2 > pet1, pet2, pet3 = ["duck","dog","cat"] # no splat operator > > # # 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")))), > > This implies to me that I should be able to define to_ary on an object, and it will be able to be RHS of a mass assign. a = Object.new def a.to_ary [1, 2] end b , c = a b # => 1 c # => 2 Yes, it works. But I am confused where to_ary came from. I have never used it myself, and wouldn't have even thought to use it except I saw it in the sexp. Googling turns up http://www.ruby-forum.com/topic/81642, which isn't helpful, and http://forums.pragprog.com/forums/54/topics/992 which left me rather unconvinced, and only addresses the difference without really addressing the reasoning. Same for the Pickaxe which says on 366 to_ary: "This is used when interpreter needs a parameter to a method to be an array, and when expanding parameters and assignments containing the *xyz syntax" to_a: "This is used when the interpreter needs to convert an object into an array for parameter passing or multiple assignment." So one's a po-tay-to, the other's a po-tah-to? In The Ruby Programming Language, on page 80, it says they are defined for the purposes of implicit conversions. I verified with this code class Fixnum alias to_str to_s end '1' + 1 # => "11" But I don't understand why the implicit version should be different from the explicit version, or why "#{obj}" doesn't invoke the implicit version. Is there some example which shows the necessity of a separate, but but similar method for to_a/to_ary, to_i/to_int, to_s/to_str, etc? --0016e68db81c84b4a704988c73f2--