Project

General

Profile

Actions

Bug #20620

open

singleton_method undefined for module using "extend self"

Added by dentarg (Patrik Ragnarsson) 9 days ago. Updated 9 days ago.

Status:
Open
Assignee:
-
Target version:
-
ruby -v:
ruby 3.4.0dev (2024-07-08T21:43:28Z master e500222de1) [x86_64-linux]
[ruby-core:118514]

Description

Is this a bug? I expected ExtendSelf.singleton_method(:foo) to work when ExtendSelf.singleton_methods worked and :foo was in the list returned.

# 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)
$ 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)

Updated by jeremyevans0 (Jeremy Evans) 9 days ago

I don't think this is a bug. It does seem inconsistent, but only because the all argument to singleton_methods defaults to true. If you call singleton_methods(false), then you get the behavior you would expect from the method name (only show actual singleton methods, not methods in modules that extend the object).

Actions

Also available in: Atom PDF

Like0
Like1