ruby-core@ruby-lang.org archive (unofficial mirror)
 help / color / mirror / Atom feed
* [ruby-core:25571] Implicit block argument in Procs
@ 2009-09-14  9:57 Cody Brocious
  2009-09-14 10:07 ` [ruby-core:25572] " Nobuyoshi Nakada
  0 siblings, 1 reply; 4+ messages in thread
From: Cody Brocious @ 2009-09-14  9:57 UTC (permalink / raw
  To: ruby-core

I ran into some block behavior I thought was a bit odd.  Calling a
Proc (via .call or any other means) with a block causes the block to
be silently dropped unless the Proc has an explicit block argument.
This occurs even when taking a block as an explicit argument to
another method and then calling that Proc.  Below is a small bit of
code which shows the issue in two ways:

# Raw Proc
proc = Proc.new { |&block|
	block.yield
}
proc.call {
	puts 'test'
}

# 'Block' Proc
def set(&block)
	$block = block
end

set {
	yield
}

$block.call {
	puts 'test'
}

From a quick glance at the source (specifically proc.c), the issue
seems to be that in proc_call() there's the following line:
if (BUILTIN_TYPE(iseq) == T_NODE || iseq->arg_block != -1) {

Inside of that block the code that handles block passing.  The
iseq->arg_block != -1 check seems to prevent passing blocks unless
they're explicitly named.

Is this behavior intentional?  If not, is there a limitation in
rb_vm_invoke_proc() which makes passing an implicit block a problem?
It seems that killing the arg_block check would solve the issue if
there's no problem in rb_vm_invoke_proc().

Thanks,
- Cody Brocious

^ permalink raw reply	[flat|nested] 4+ messages in thread

* [ruby-core:25572] Re: Implicit block argument in Procs
  2009-09-14  9:57 [ruby-core:25571] Implicit block argument in Procs Cody Brocious
@ 2009-09-14 10:07 ` Nobuyoshi Nakada
  2009-09-14 10:13   ` [ruby-core:25573] " Cody Brocious
  0 siblings, 1 reply; 4+ messages in thread
From: Nobuyoshi Nakada @ 2009-09-14 10:07 UTC (permalink / raw
  To: ruby-core

Hi,

At Mon, 14 Sep 2009 18:57:14 +0900,
Cody Brocious wrote in [ruby-core:25571]:
> Is this behavior intentional?  If not, is there a limitation in
> rb_vm_invoke_proc() which makes passing an implicit block a problem?
> It seems that killing the arg_block check would solve the issue if
> there's no problem in rb_vm_invoke_proc().

Absolutely intentional.  `yield' calls the block given to the
context.  In your example, the yield in the block to `set' is
executed in the top level context, which has no block.

-- 
Nobu Nakada

^ permalink raw reply	[flat|nested] 4+ messages in thread

* [ruby-core:25573] Re: Implicit block argument in Procs
  2009-09-14 10:07 ` [ruby-core:25572] " Nobuyoshi Nakada
@ 2009-09-14 10:13   ` Cody Brocious
  2009-09-15 12:28     ` [ruby-core:25601] " Nobuyoshi Nakada
  0 siblings, 1 reply; 4+ messages in thread
From: Cody Brocious @ 2009-09-14 10:13 UTC (permalink / raw
  To: ruby-core

Thanks for the quick response.  If the block to 'set' is executed in
the top level context, though, why is it that an explicit block
argument works?  It seems that unless you're passing blocks into Procs
that assume no implicit block argument exists, this wouldn't change
the usual behavior.

On Mon, Sep 14, 2009 at 6:07 AM, Nobuyoshi Nakada <nobu@ruby-lang.org> wrote:
> Hi,
>
> At Mon, 14 Sep 2009 18:57:14 +0900,
> Cody Brocious wrote in [ruby-core:25571]:
>> Is this behavior intentional?  If not, is there a limitation in
>> rb_vm_invoke_proc() which makes passing an implicit block a problem?
>> It seems that killing the arg_block check would solve the issue if
>> there's no problem in rb_vm_invoke_proc().
>
> Absolutely intentional.  `yield' calls the block given to the
> context.  In your example, the yield in the block to `set' is
> executed in the top level context, which has no block.
>
> --
> Nobu Nakada
>
>

^ permalink raw reply	[flat|nested] 4+ messages in thread

* [ruby-core:25601] Re: Implicit block argument in Procs
  2009-09-14 10:13   ` [ruby-core:25573] " Cody Brocious
@ 2009-09-15 12:28     ` Nobuyoshi Nakada
  0 siblings, 0 replies; 4+ messages in thread
From: Nobuyoshi Nakada @ 2009-09-15 12:28 UTC (permalink / raw
  To: ruby-core

Hi,

At Mon, 14 Sep 2009 19:13:08 +0900,
Cody Brocious wrote in [ruby-core:25573]:
> Thanks for the quick response.  If the block to 'set' is executed in
> the top level context, though, why is it that an explicit block
> argument works?  It seems that unless you're passing blocks into Procs
> that assume no implicit block argument exists, this wouldn't change
> the usual behavior.

Proc#yield and keyword `yield' are different things.

-- 
Nobu Nakada

^ permalink raw reply	[flat|nested] 4+ messages in thread

end of thread, other threads:[~2009-09-15 12:28 UTC | newest]

Thread overview: 4+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2009-09-14  9:57 [ruby-core:25571] Implicit block argument in Procs Cody Brocious
2009-09-14 10:07 ` [ruby-core:25572] " Nobuyoshi Nakada
2009-09-14 10:13   ` [ruby-core:25573] " Cody Brocious
2009-09-15 12:28     ` [ruby-core:25601] " Nobuyoshi Nakada

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).