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é) 8 months ago. Updated 8 months 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) 8 months 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