Project

General

Profile

Actions

Feature #19118

closed

Allow numbered arguments to be inherited by block

Added by 0x1eef (0x 1eef) over 1 year ago. Updated over 1 year ago.

Status:
Closed
Assignee:
-
Target version:
-
[ruby-core:110684]

Description

Each these examples does not work as I would have hoped - each of them evaluates to puts(nil).

def foo
  yield
end
[1,2,3].each do
  Process.wait fork { puts(_1) }
end
[1,2,3].each do
  Thread.new { puts(_1) }.join
end
[1,2,3].each do
  foo { puts(_1) }
end

Is it possible for numbered parameters to be inherited if the inner block is yielded with no arguments ?

Thanks!

Updated by matz (Yukihiro Matsumoto) over 1 year ago

  • Status changed from Open to Closed

I am sorry, but I don't think it's possible. fork or Thread.new may pass some values to the block that should be referenced by its own numbered parameters.

We have no way to distinguish whether it's possible to inherit outer ones or not.

Matz.

Updated by 0x1eef (0x 1eef) over 1 year ago

Alright - no worries. Thank you!

Updated by austin (Austin Ziegler) over 1 year ago

matz (Yukihiro Matsumoto) wrote in #note-1:

I am sorry, but I don't think it's possible. fork or Thread.new may pass some values to the block that should be referenced by its own numbered parameters.

We have no way to distinguish whether it's possible to inherit outer ones or not.

This is entirely tongue-in-cheek, but we could count the number of underscores…

[1,2,3].each do
  foo { puts(__1) }
end

And if the it proposal is ever accepted, we could count the number of is:

[1, 2, 3].each do
  foo { puts iit }
end
Actions

Also available in: Atom PDF

Like0
Like0Like0Like1