[ruby-dev:51089] [Ruby master Bug#18069] `instance_exec` is just ignored when the block is originally a method
From:
info@...
Date:
2021-08-08 14:30:32 UTC
List:
ruby-dev #51089
Issue #18069 has been reported by ttanimichi (Tsukuru Tanimichi).
----------------------------------------
Bug #18069: `instance_exec` is just ignored when the block is originally a method
https://bugs.ruby-lang.org/issues/18069
* Author: ttanimichi (Tsukuru Tanimichi)
* Status: Open
* Priority: Normal
* Backport: 2.6: UNKNOWN, 2.7: UNKNOWN, 3.0: UNKNOWN
----------------------------------------
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.
```ruby
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)
```
--
https://bugs.ruby-lang.org/