From: "David A. Black" Date: 2009-02-25T23:37:09+09:00 Subject: Re: Where is splat implemented? / How does it work? Hi -- Mischa Fierer wrote: > Hi -- > > After a good amount of digging, I'm still not sure how [*[1,2]] == > [1,2]. I understand what it does, but I want to know why. > > I had originally assumed it was a normal method (like + or -), but it > appears to be implemented at a deeper level. It looks like rubinius does > something with cast_array. Ruby 1.8 does to_ary and ruby 1.9 does > to_splat. Where does this happen? to_ary says that it just returns self, > so how does self get exploded? > > Short of reading eval.c, does anyone have any pointers? It ties itself to to_a: >> obj = Object.new => # >> def obj.to_a; [1,2,3]; end => nil >> a = *obj => [1, 2, 3] That code works the same in 1.8 and 1.9.1. There may be some classes where it's optimized away so that you can't override it even by defining to_a. I'm not sure. David -- David A. Black / Ruby Power and Light, LLC Ruby/Rails consulting & training: http://www.rubypal.com Coming in 2009: The Well-Grounded Rubyist (http://manning.com/black2) Ruby Training Atlanta! April 1-3, http://www.entp.com/training/atlanta09