2

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
4

1 回答 1

2

你在正确的轨道上。

1)在单例类中查找方法时,您想使用instance_methods,而不是methods

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

这有点令人困惑,因为method_defined?真的意味着“定义了实例方法?”,而 methods真的意味着singleton methods......

2) 你不能继承或实例化一个单例类,因为它是一个单例,即只有一个实例。

无论如何,这并不重要,因为您应该将 mixins 用于您想要重用的代码。这些可以包含/添加到任意数量的单例类或普通类中:

foo.extend ResuableFunctionality
# or equivalently:
class << foo
  include ReusableFunctionality
end
于 2013-03-16T16:31:13.160 回答