From: "jeremyevans0 (Jeremy Evans) via ruby-core" <ruby-core@...>
Date: 2024-09-12T14:23:37+00:00
Subject: [ruby-core:119138] [Ruby master Bug#20620] singleton_method undefined for module using "extend self"

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


I submitted a pull request to implement this: https://github.com/ruby/ruby/pull/11605

In terms of compatibility issues:

* For singleton classes that prepend a module, and have the method defined in both the singleton class and prepended module, `singleton_method` now returns the method in the prepended module.

* Cases where a method is defined in a module prepended to or included in the singleton classes and not in the singleton class itself now result in a `Method` Object being returned, instead of raising `NameError`.

Use of `singleton_method` isn't that common in my experience, so I think we should make the change.  There is still time to back it out of 3.4 if it causes issues.

----------------------------------------
Bug #20620: singleton_method undefined for module using "extend self"
https://bugs.ruby-lang.org/issues/20620#change-109727

* Author: dentarg (Patrik Ragnarsson)
* Status: Open
* ruby -v: ruby 3.4.0dev (2024-07-08T21:43:28Z master e500222de1) [x86_64-linux]
* Backport: 3.1: UNKNOWN, 3.2: UNKNOWN, 3.3: UNKNOWN
----------------------------------------
Is this a bug? I expected `ExtendSelf.singleton_method(:foo)` to work when `ExtendSelf.singleton_methods` worked and `:foo` was in the list returned.

```ruby
# singleton_method.rb
class Klass
  def self.foo
  end
end

module ExtendSelf
  extend self

  def foo
  end
end

p RUBY_DESCRIPTION

p "Klass"
p Klass.singleton_methods      # => [:foo]
p Klass.singleton_method(:foo) # => #<Method: Klass.foo() singleton_method.rb:2>

p "ExtendSelf"
p ExtendSelf.singleton_methods      # => [:foo]
p ExtendSelf.singleton_method(:foo) # => singleton_method.rb:19:in `singleton_method': undefined singleton method `foo' for `ExtendSelf' (NameError)
```

```shell
$ docker run --rm -it -v $(pwd):/app -w /app rubylang/ruby:master-nightly-jammy
Unable to find image 'rubylang/ruby:master-nightly-jammy' locally
master-nightly-jammy: Pulling from rubylang/ruby
...
Digest: sha256:922037b184b897786a26b2460f70a66139f87dce67f20ddf760cd4186790ed9f

root@9807305c322a:/app# ruby -v
ruby 3.4.0dev (2024-07-08T21:43:28Z master e500222de1) [x86_64-linux]

root@9807305c322a:/app# ruby singleton_method.rb
"ruby 3.4.0dev (2024-07-08T21:43:28Z master e500222de1) [x86_64-linux]"
"Klass"
[:foo]
#<Method: Klass.foo() singleton_method.rb:2>
"ExtendSelf"
[:foo]
singleton_method.rb:21:in 'Kernel#singleton_method': undefined singleton method 'foo' for 'ExtendSelf' (NameError)
```



-- 
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/lists/ruby-core.ml.ruby-lang.org/