Project

General

Profile

Actions

Bug #18067

closed

Composite procs with `instance_exec` aren't executed within the context of the receiver

Added by ttanimichi (Tsukuru Tanimichi) over 2 years ago. Updated over 2 years ago.

Status:
Rejected
Assignee:
-
Target version:
-
[ruby-dev:51088]

Description

func1 = -> (x) { x + y }

class A
  def y
    10
  end
end

A.new.instance_exec(1, &func1) # => 11

func2 = -> (x) { x * 2 }
f = func1 >> func2
A.new.instance_exec(1, &f) # => undefined local variable or method `y' for main:Object (NameError)

Related issues 1 (1 open0 closed)

Has duplicate Ruby master - Feature #18815: instance_{eval,exec} vs Proc#>>OpenActions

Updated by jeremyevans0 (Jeremy Evans) over 2 years ago

I think this is expected. You get the same error if you compose the proc manually, and the proc composition operators should reflect the same behavior as manual composition:

func1 = -> (x) { x + y }

class A
  def y
    10
  end
end

A.new.instance_exec(1, &func1) # => 11

func2 = -> (x) { x * 2 }
f = ->(x) { func2.(func1.(x)) }
A.new.instance_exec(1, &f) # => undefined local variable or method `y' for main:Object (NameError)
Actions #2

Updated by jeremyevans0 (Jeremy Evans) over 2 years ago

  • Status changed from Open to Rejected
Actions #3

Updated by jeremyevans0 (Jeremy Evans) almost 2 years ago

Actions

Also available in: Atom PDF

Like0
Like0Like0Like0