From: Robert Klemme Date: 2004-12-01T01:52:48+09:00 Subject: Re: Unpacking an array with the star operator "Brian Schr�der" schrieb im Newsbeitrag news:20041130174018.40e55108@black.wg... > Hello Group, > > I just noticed the following behaviour: > > $ irb --prompt-mode xmp > *[:x] > SyntaxError: compile error > (irb):1: syntax error > from (irb):1 > a=*[:x] > ==>:x > > and wondered why it is the case. I always understood the * operator as pack/unpack array operator. So I would have expected the following behaviour. > > *[:x] => :x No, it does not pack / unpack an array. You can only use it in a situation where there is an assignment. The star indicates, that a given value should be enumerated and assigned to lvalues in order (this applies also to method argument lists). > What is the reason for * being implemented as it is? > > After reading this again I see, that it may be the problem with > > *[:x, :y] => ??? No, it doesn't matter how many elements you put into the array. > So in fact maybe I should not wonder. So let me rephrase it into "what exactly does the * return", because I thought "every ruby expressions retruns something" and a = *[:x] are two expressions. It's not exactly an expression that returns something. It's more like a compiler directive - if you look for an analogy. > And is the * a private kernel method or where is it defined? I'd guess that it's implemented somewhere in the Ruby interpreter - not in any particular class or module - as it is *not* an operator in the usual meaning of the word. > PS: Sorry if this is a dumb question. I don't think it's a dumb question. Kind regards robert PS: Some examples: 17:46:05 [robert.klemme]: irbs >> class Foo >> include Enumerable >> def each() yield "foo" end >> end => nil >> Foo.new.to_a => ["foo"] >> a,b=*Foo.new => ["foo"] >> a => "foo" >> b => nil >> a,b=Foo.new => [#] >> a => # >> b => nil