Actions
Bug #19888
closedCan't change binding of a Proc coerced from a Method
Bug #19888:
Can't change binding of a Proc coerced from a Method
Description
instance_exec or instance_eval can execute a given Proc in the context of the receiver. However, that's not true when the Proc has been coerced from a Method. In that situation, the binding is always tied to the original instance.
class A
def foo
"from A"
end
end
class B
def foo
"from B"
end
end
A.new.instance_exec(&-> { foo }) # => from A
A.new.instance_exec(&B.new.method(:foo)) # => from B
A.new.instance_exec(&B.new.method(:foo).to_proc) # => from B
At first sight, that looks like a bug, as the transformation from Method to Proc is not complete.
Updated by nobu (Nobuyoshi Nakada) about 2 years ago
- Status changed from Open to Feedback
Converting Method to Proc is not copying the inner code literally into a new block.
Method is bound to the receiver, while UnboundMethod returned by Module#instance_method is not, as the names.
Actions