Project

General

Profile

Actions

Bug #19888

closed

Can't change binding of a Proc coerced from a Method

Added by waiting_for_dev (Marc Busqué) over 1 year ago. Updated over 1 year ago.

Status:
Feedback
Assignee:
-
Target version:
-
[ruby-core:114795]

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) over 1 year 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

Also available in: Atom PDF

Like0
Like0