Project

General

Profile

Actions

Feature #18069

open

`instance_exec` is just ignored when the block is originally a method

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

Status:
Open
Assignee:
-
Target version:
-
[ruby-dev:51089]

Description

I know you can't instance_exec a proc which is generated by Method#to_proc because it has its original instance's context. But, in such a case, raising ArgumentError would be the ideal behavior.

f = -> (x) { a + x }

class A
  def a
    1
  end
end

A.new.instance_exec(1, &f) # => 2

class B
  def b(x)
    a + x
  end
end

proc = B.new.method(:b).to_proc
A.new.instance_exec(1, &proc) # => undefined local variable or method `a' for #<B:0x00007fdaf30480a0> (NameError)
Actions

Also available in: Atom PDF

Like0
Like0Like0