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) 3 months ago. Updated 17 days ago.

Status:
Rejected
Priority:
Normal
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)

Updated by jeremyevans0 (Jeremy Evans) 3 months 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) 17 days ago

  • Status changed from Open to Rejected
Actions

Also available in: Atom PDF