红宝石中的单身人士究竟是什么类别?

时间:2008-10-17 14:35:36

标签: ruby oop singleton

Ruby中的单例类本身是一个类吗?这就是为什么所有对象都属于“类”的原因吗?概念是模糊,但我认为它与我为什么可以定义一个类方法(class foo; def foo.bar ...)有关。

Ruby中的单例类是什么?

5 个答案:

答案 0 :(得分:128)

首先,一个小定义:单例方法是仅为单个对象定义的方法。例如:

irb(main):001:0> class Foo; def method1; puts 1; end; end
=> nil
irb(main):002:0> foo = Foo.new
=> #<Foo:0xb79fa724>
irb(main):003:0> def foo.method2; puts 2; end
=> nil
irb(main):004:0> foo.method1
1
=> nil
irb(main):005:0> foo.method2
2
=> nil
irb(main):006:0> other_foo = Foo.new
=> #<Foo:0xb79f0ef4>
irb(main):007:0> other_foo.method1
1
=> nil
irb(main):008:0> other_foo.method2
NoMethodError: undefined method `method2' for #<Foo:0xb79f0ef4>
        from (irb):8

实例方法是类的方法(即在类的定义中定义)。类方法是类的Class实例上的单例方法 - 它们未在类的定义中定义。相反,它们是在对象的单例类上定义的。

irb(main):009:0> Foo.method_defined? :method1
=> true
irb(main):010:0> Foo.method_defined? :method2
=> false

使用语法class << obj打开对象的单例类。在这里,我们看到这个单例类是定义单例方法的地方:

irb(main):012:0> singleton_class = ( class << foo; self; end )
=> #<Class:#<Foo:0xb79fa724>>
irb(main):013:0> singleton_class.method_defined? :method1
=> true
irb(main):014:0> singleton_class.method_defined? :method2
=> true
irb(main):015:0> other_singleton_class = ( class << other_foo; self; end )
=> #<Class:#<Foo:0xb79f0ef4>>
irb(main):016:0> other_singleton_class.method_defined? :method1
=> true
irb(main):017:0> other_singleton_class.method_defined? :method2
=> false

因此,向对象添加单例方法的另一种方法是在对象的单例类打开的情况下定义它们:

irb(main):018:0> class << foo; def method3; puts 3; end; end
=> nil
irb(main):019:0> foo.method3
3
=> nil
irb(main):022:0> Foo.method_defined? :method3
=> false

总结:

  • 方法必须始终属于某个类(或:是某类的实例方法)
  • 普通方法属于它们所定义的类(即类的实例方法)
  • 类方法只是Class
  • 的单例方法
  • 对象的单例方法不是对象类的实例方法;相反,它们是对象的单例类的实例方法。

答案 1 :(得分:23)

Ruby提供了一种定义特定于特定对象的方法的方法,这种方法称为单例方法。当一个人在一个对象上声明一个单例方法时,Ruby会自动创建一个只保存单例方法的类。新创建的类称为Singleton Class。


    foo = Array.new
    def foo.size
      "Hello World!"
    end
    foo.size  # => "Hello World!"
    foo.class # => Array
    #Create another instance of Array Class and call size method on it
    bar = Array.new
    bar.size  # => 0
Singleton类是特定于对象的匿名类,它自动创建并插入到继承层次结构中。

可以在对象上调用

singleton_methods来获取对象上所有单例方法的名称列表。

    foo.singleton_methods  # => [:size]
    bar.singleton_methods  # => []

这个article真的帮助我理解了Ruby中的Singleton类,它有一个很好的代码示例。

答案 2 :(得分:6)

刚刚更新到@Pistos答案,从版本1.9.2 ruby​​添加新语法以获取单例类

 singleton_class = ( class << foo; self; end )

可以替换为:

singleton_class = foo.singleton_class

https://apidock.com/ruby/Object/singleton_class

答案 3 :(得分:4)

最实用/最有效的方式(IMHO)是:作为继承链或方法查找/解析顺序。这张照片可能会有所帮助

http://www.klankboomklang.com/2007/11/25/modules-part-i-enter-the-include-class/

这是r 1.9,对比内置和用户定义的类:我还在消化这个类。

http://d.hatena.ne.jp/sumim/20080111/p1

另外,我认为这个术语的混淆使用是“Singleton对象”,这是不同的概念。单例对象来自一个类,它重写了构造函数/实例化方法,因此您只能分配该类中的一个。

答案 4 :(得分:0)

用最简单的术语来说,单例类是特殊类的红宝石鞭子,用于承载在单个对象上定义的方法。在ruby中,可以在单个对象上定义方法,而该方法对于该对象是唯一的。例如,请考虑以下内容

class User; end
user = User.new
def user.age
  "i'm a unique method"
end
user1 = User.new 
user.age #"i'm a unique method"
user1.age # NoMethodError (undefined method `age' for #<User:0x0000559c66ab7338>)

正如您在上面看到的,user1对象不响应'age'方法,因为它是单例方法,这是在用户对象上唯一定义的方法。为此,ruby创建一个特殊的类(称为单例类或本征类)来承载此唯一方法。您可以通过执行以下操作来验证这一点:

user.singleton_class # #<Class:#<User:0x0000559c66b47c58>>

您还可以询问ruby,方法是使用方法对象找出“ age”方法的定义位置,从而找到“ age”方法。当您执行此操作时,您将看到单例类具有该方法。

user_singleton_class = user.method(:age).owner # #<Class:#<User:0x0000559c66b47c58>>
user.method(:age).owner == user.singleton_class # true
user_singleton_class.instance_methods(false) # [:age]

还要注意,就单例类而言,单例方法实际上是其实例方法。

user.singleton_methods == user_singleton_class.instance_methods(false) # true
相关问题