[#863] RDtool-0.5.0 — Toshiro Kuwabara <toshirok@...3.so-net.ne.jp>

Hi,

18 messages 1999/10/23
[#864] Re: RDtool-0.5.0 — matz@... (Yukihiro Matsumoto) 1999/10/26

Hi,

[ruby-talk:00871] call with a Proc

From: Clemens Hintze <c.hintze@...>
Date: 1999-10-28 06:43:08 UTC
List: ruby-talk #871
ts writes:
> 
> 
>  Hi,

Hi too,

> 
>  I've some problem to understand how ruby pass its argument, specially when
>  it has a proc has parameter.

not too diffcult, you will see!

> 
>  I've tried this little example (because list() take (*args, &block) as
>  arguments), what I'm doing wrong ? 

Okay! If a method takes a '&' parameter that means, it waits for a
block! That means, list *could* be called like:

   b.list(...) { <do something...> };

If it is called that way, the block will converted into a Proc
instance *and* assigned to the '&' parameter (called '&block' here)!

If it is not called that way, the '&' parameter receives 'nil'!!!!

All paramters of the parameter list, enclosed in '('...')', will be
assigned to '*' parameter (called '*args' here).

As the methods 'proc', 'lambda' and 'Proc.new' will takes a block, and
converts it into a 'Proc' instance, they *consume* that block! There
is no block afterwards!!!

That means, if you call:

   b.list(..., Proc.new{ <do something> });

'Proc.new' takes the block, converts it into a Proc instance. So the
method call would like that:

   blk = Proc.new { <do something>... };
   b.list(..., blk);

As you can see, there is doesn't remain any block during method
call. So the '&block' argument of method 'list' is 'nil' here!!!

But the 'Proc' instance you have created via 'Proc.new' will be passed
as normal argument, and as such it will be assigned to the '*args'
list! As 'list' try to cat all its parameter together, it will fail to
concat a 'Proc' instance to a string! Right? :-)))

[...]

> 
> Guy Decoux

HTH,
Clemens Hintze.

-- 
Clemens Hintze  mailto: c.hintze@gmx.net

In This Thread