From: "David A. Black" Date: 2010-04-15T08:51:21+09:00 Subject: Re: I thought spaces didn't matter around operators Hi -- On Wed, 14 Apr 2010, Sarah Allen wrote: > David A. Black wrote: >> Here's another interesting space/non-space case: >> >> >> def m(*args); "hi"; end >> => nil >> >> m *10 >> => "hi" >> >> m * 10 >> => "hihihihihihihihihihi" >> >> In m *10 the * is the unary *, while in m * 10 it's the infix >> operator/method *. >> >> >> David > > And what, may I ask, it the unary * operator? Is it what I've heard > Rubyists call the "splat" operator that you use in var args? How and > why would you override it? It is indeed what people call the "splat" operator, though I've never found that name very expressive. I think of it as the unar[r]ay operator (that's unary unarray :-) It does an "unarray" operation in the sense that it can turn an array into a bare list, in a method call. You can't define it directly (I believe that's true in 1.9 as well as earlier versions), but you can affect what it does via the #to_a method: o = Object.new def o.to_a; [1,2,3]; end a = *o p a # [1,2,3] As for the why: I don't think you'd often define #to_a just to get * behavior. It's more of an extra thing you get in cases where you need #to_a anyway. David -- David A. Black Senior Developer, Cyrus Innovation Inc. THE COMPLEAT RUBYIST, Ruby training with Black/Brown/McAnally! http://www.compleatrubyist.com