Lloyd Zusman <ljz / asfast.com> wrote:
> "Hee-Sob Park" <phasis / hananet.net> writes:
> 
> > "Lloyd Zusman" <ljz / asfast.com> wrote in message
> > news:ltofuiikge.fsf / asfast.com...
> > > 
> > > [ ... ]
> > 
> > How about this?
> > 
> > cmd = ['/bin/ls',*ARGV]
> > exec(*cmd)
> 
> Thank you, that works fine!  I never tried it with the `*' before
> `ARGV'.
> 
> Unless I overlooked something, I couldn't find an explanation for `*'
> (as used above) anywhere in "Programming Ruby", which only discusses
> it in the following context:
> 
>   def varargs(a, *b)
>     [ a, b ]
>   end
>   varargs 1         ->  [ 1, [] ]
>   varargs 1, 2      ->  [ 1, [ 2 ] ]
>   varargs 1, 2, 3   ->  [ 1, [ 2, 3 ] ]
> 
> 
> The above example shows `*' being used within a method definition,
> not a method invocation, and I'd like to understand the exact
> meaning of `*' when used this way in the argument list of a *call*
> to a method.  Does it mean this: "append the following array as a
> series of arguments to the set of arguments that are being passed to
> the method whose parameter list I appear in"?  That would explain
> why it works here:
> 
>   cmd = ['/bin/ls', *ARGV]
> 
> .. because the [] operator actually represents an invocation of a
> method.  Is what I wrote here a correct explanation of `*' in this
> context?
> 
> Thanks to all of you for your help.

Yep, that's exactly what it means.  Actually, it can happen in any one of 
several contexts, but the most important one is that it "flattens" an Array 
out into method arguments, just like you want.

E.g., try this:
module Kernel
    def mysend(meth, *args, &block)
        p "mysend: #{meth.inspect} #{args.inspect} #{block.inspect}"
        __send__(meth, *args, &block)
    end
end

$stdout.mysend(:puts,
"Using * and & forms in the method definition coerces the
argument list past that point or the block given
(respectively) into a single variable.  Using them in
a method call reverses this coercion, transforming
*args into a parameter list and &block into a normal block.", "See?") {}

-- 
 Brian Fundakowski Feldman           \  FreeBSD: The Power to Serve!  /
 green / FreeBSD.org                    `------------------------------'