-->

Where does `singleton` methods reside in Ruby?

2020-07-27 07:08发布

问题:

I was playing with singleton class in my IRB. And doing so tried the below snippets.

class Foo ; end
#=> nil
foo = Foo.new
#=> #<Foo:0x2022738>

foo.define_singleton_method(:bar , method(:puts))
#=> #<Method: Object(Kernel)#puts>

Here above I just created a singleton method on instance of class Foo.

foo.bar("hi")
hi
#=> nil
foo.singleton_methods
#=> [:bar]

foo_sing = foo.singleton_class
#=> #<Class:#<Foo:0x2022738
foo_sing.is_a? Class
#=> true
foo_sing.instance_of? Class
#=> true
foo_sing.inspect
#=> "#<Class:#<Foo:0x1e06dc8>>"

In the above I tried to create a singleton class on instance of class Foo. And also tested if foo_sing holds reference to the singleton class on instance of class Foo.

foo_sing.methods(false)
#=> []
foo_sing.methods.include? :bar
#=> false

In the above I was looking if singleton_method bar is in the foo_sing or not.But found it is not present there.Then my question is - where does thosesingleton_method reside in Ruby?

foo_sing.new.methods(false)
#TypeError: can't create instance of singleton class
#        from (irb):10:in `new'
#        from (irb):10
#        from C:/Ruby193/bin/irb:12:in `<main>'
class Bas < foo_sing ; end
#TypeError: can't make subclass of singleton class
#        from (irb):16
#        from C:/Ruby193/bin/irb:12:in `<main>'

In the above part I was checking if we can create,instance of singleton class or not and subclass of singleton class,like regular class. But the answers I found as NO. What is the concept or theory or purpose behind it ?

Again in the following code I can see the that same name methods are being overridden inside a singleton class. But when I am searching that method inside the class not found as I asked above.

class Foo ; end
#=> nil
foo = Foo.new
#=> #<Foo:0x225e3a0>

def foo.show ; puts "hi" ; end
#=> nil

foo.show
#hi
#=> nil

class << foo ;def show ; puts "hello" ; end ;end
#=> nil

foo.show
#hello
#=> nil

回答1:

You're on the right track.

1) When looking for the method in the singleton class, you wanted to use instance_methods, not methods:

foo_sing.instance_methods.include? :bar # => true
# or
foo_sing.method_defined? :bar # => true

It is a bit confusing, since method_defined? really means "instance methods defined?", while methods really means singleton methods...

2) You can't subclass or instantiate a singleton class because it is meant to be a singleton, i.e. have exactly one instance.

It doesn't matter anyways, since you should instead use mixins for code that you want to reuse. These can be included/prepended in as many singleton classes or normal classes as you want:

foo.extend ResuableFunctionality
# or equivalently:
class << foo
  include ReusableFunctionality
end