ruby-dev (Japanese) list archive (unofficial mirror)
 help / color / mirror / Atom feed
* [ruby-dev:51089] [Ruby master Bug#18069] `instance_exec` is just ignored when the block is originally a method
@ 2021-08-08 14:30 info
  2021-08-09 19:00 ` [ruby-dev:51091] [Ruby master Feature#18069] " merch-redmine
  2021-08-10  5:33 ` [ruby-dev:51092] " nobu
  0 siblings, 2 replies; 3+ messages in thread
From: info @ 2021-08-08 14:30 UTC (permalink / raw)
  To: ruby-dev

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/

^ permalink raw reply	[flat|nested] 3+ messages in thread

* [ruby-dev:51091] [Ruby master Feature#18069] `instance_exec` is just ignored when the block is originally a method
  2021-08-08 14:30 [ruby-dev:51089] [Ruby master Bug#18069] `instance_exec` is just ignored when the block is originally a method info
@ 2021-08-09 19:00 ` merch-redmine
  2021-08-10  5:33 ` [ruby-dev:51092] " nobu
  1 sibling, 0 replies; 3+ messages in thread
From: merch-redmine @ 2021-08-09 19:00 UTC (permalink / raw)
  To: ruby-dev

Issue #18069 has been updated by jeremyevans0 (Jeremy Evans).

Backport deleted (2.6: UNKNOWN, 2.7: UNKNOWN, 3.0: UNKNOWN)
Tracker changed from Bug to Feature

I don't think the current behavior is a bug. `Method#to_proc` is currently equivalent to:

```ruby
class Method
  def to_proc
    method = self
    ->(*args, **kwargs, &block) do
      method.call(*args, **kwargs, &block)
    end
  end
end
```

You wouldn't expect an `instance_exec` on that lambda to change the behavior of `Method#call`. So I think the current behavior is expected.

Note that it's not hard to change the behavior to raise an error in this case (and other cases like `module_exec`).  However, changing the behavior would result in significant backwards compatibility issues.  I tried a commit that raises ArgumentError in such a case: https://github.com/jeremyevans/ruby/commit/3e2db2f01281f2335c638142223f8b24531826bd.  However, it broke quite a few tests: https://github.com/jeremyevans/ruby/runs/3283493124. Some of the breakage may be due to implementation choice, but I checked and at least some of the breakage is unavoidable as the tests expect to pass procs created by `Method#to_proc` to `instance_exec` (e.g. `test_instance_exec_define_method_kwsplat`).

As I don't think this is a bug, I'm switching this to a feature request.

----------------------------------------
Feature #18069: `instance_exec` is just ignored when the block is originally a method
https://bugs.ruby-lang.org/issues/18069#change-93200

* Author: ttanimichi (Tsukuru Tanimichi)
* Status: Open
* Priority: Normal
----------------------------------------

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/

^ permalink raw reply	[flat|nested] 3+ messages in thread

* [ruby-dev:51092] [Ruby master Feature#18069] `instance_exec` is just ignored when the block is originally a method
  2021-08-08 14:30 [ruby-dev:51089] [Ruby master Bug#18069] `instance_exec` is just ignored when the block is originally a method info
  2021-08-09 19:00 ` [ruby-dev:51091] [Ruby master Feature#18069] " merch-redmine
@ 2021-08-10  5:33 ` nobu
  1 sibling, 0 replies; 3+ messages in thread
From: nobu @ 2021-08-10  5:33 UTC (permalink / raw)
  To: ruby-dev

Issue #18069 has been updated by nobu (Nobuyoshi Nakada).


A method is bound to the internal states of the receiver.
I don't think that removing the receiver from a method makes sense.

----------------------------------------
Feature #18069: `instance_exec` is just ignored when the block is originally a method
https://bugs.ruby-lang.org/issues/18069#change-93211

* Author: ttanimichi (Tsukuru Tanimichi)
* Status: Open
* Priority: Normal
----------------------------------------

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/

^ permalink raw reply	[flat|nested] 3+ messages in thread

end of thread, other threads:[~2021-08-10  5:33 UTC | newest]

Thread overview: 3+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2021-08-08 14:30 [ruby-dev:51089] [Ruby master Bug#18069] `instance_exec` is just ignored when the block is originally a method info
2021-08-09 19:00 ` [ruby-dev:51091] [Ruby master Feature#18069] " merch-redmine
2021-08-10  5:33 ` [ruby-dev:51092] " nobu

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).