On May 16, 2006, at 10:17 PM, travis michel wrote:
> #!/usr/bin/env ruby
>
> x = Proc.new { |a| p( block_given? ? yield(a) : a )  }
> x.call( 'World' ) { |b| p "Hello #{b}" }

I don't believe you can pass a block argument when calling a Proc.   
It is silently ignored.
In your case the call to block_given? would be relative to the scope  
where Proc.new is called and not relative to the arguments passed  
when the block is executed via Proc#call.  This is changing in Ruby  
1.9 I believe so that block arguments can be provided to blocks when  
called via yield and/or Proc#call.

> # Likewise, in continuation:
>
> y = Proc.new { |b| p "Hello #{b}" }
> p x.call( 'World', &y )

Same problem, a block argument is ignored by Proc#call.

> # this may be related, it returns false instead of nil... what gives?
>
> p x.call( 'World' ) &y

x.call('World') returns nil because the return value of the Proc is  
the last expression evaluated in the block which is a call to  
Kernel#p, which always returns nil.

So now you have:

p nil &y

which is getting parsed as a call to the bitwise and operator (nil &  
y), which returns false.


Gary Wright