From: "nobu (Nobuyoshi Nakada) via ruby-core" Date: 2023-09-18T13:23:17+00:00 Subject: [ruby-core:114797] [Ruby master Bug#19888] Can't change binding of a Proc coerced from a Method Issue #19888 has been updated by nobu (Nobuyoshi Nakada). 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. ---------------------------------------- Bug #19888: Can't change binding of a Proc coerced from a Method https://bugs.ruby-lang.org/issues/19888#change-104641 * Author: waiting_for_dev (Marc Busqu�) * Status: Feedback * Priority: Normal * Backport: 3.0: UNKNOWN, 3.1: UNKNOWN, 3.2: UNKNOWN ---------------------------------------- `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. ```ruby 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. -- https://bugs.ruby-lang.org/ ______________________________________________ ruby-core mailing list -- ruby-core@ml.ruby-lang.org To unsubscribe send an email to ruby-core-leave@ml.ruby-lang.org ruby-core info -- https://ml.ruby-lang.org/mailman3/postorius/lists/ruby-core.ml.ruby-lang.org/