Project

General

Profile

Actions

Bug #21123

closed

instance_exec with curried proc

Added by taichi730 (Taichi Ishitani) 3 months ago. Updated 3 months ago.

Status:
Rejected
Assignee:
-
Target version:
-
ruby -v:
3.4.1 (2024-12-25 revision 48d4efcb85) +PRISM [x86_64-linux]
[ruby-dev:<unknown>]

Description

I have a question about behavior of #instance_exec with a curried proc.

When running #instance_exex with a curried proc, it appears that the given proc is executed on the context where it is created but not the receiver object.

Example code:

a = Object.new
def a.foo(n)
  p
end

b = proc { |n| foo(n) }.curry
a.instance_exec(0, &b)

Execution result:

$ ruby test.rb
test.rb:6:in 'block in <main>': undefined method 'foo' for main (NoMethodError)

b = proc { |n| foo(n) }.curry
               ^^^
Did you mean?  for
        from test.rb:7:in 'BasicObject#instance_exec'
        from test.rb:7:in '<main>'

Is this intentional behavior?
I expect that the given curried proc is also executed on the context of the receiver object like a non-curried proc.

Actions #1

Updated by mame (Yusuke Endoh) 3 months ago

I believe this behavior is by design.

Proc#curry returns a wrapper Proc that calls to a given Proc. In other words, your code is essentially the same as the following code.

a = Object.new
def a.foo(n)
  p
end

b = proc { |n| foo(n) }
b2 = proc { |n| b.call(n) }
a.instance_exec(0, &b2)

instance_exec replaces self in the context of Proc b2, but it does not propagate to the context of Proc b.

Actions #2

Updated by taichi730 (Taichi Ishitani) 3 months ago

Hi @mame (Yusuke Endoh) san,

Thanks for your explanation and I understood the reason of this behavior.
I have no reasonable thought but I think it would be nice that curried blocks are able to be executed by #instance_exec.

Actions #3

Updated by byroot (Jean Boussier) 3 months ago

  • Status changed from Open to Rejected

I agree with @mame (Yusuke Endoh) this isn't a bug, and by design.

And I don't see how it could reasonably be changed without breaking lots of code.

Actions

Also available in: Atom PDF

Like0
Like0Like0Like0