Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/oop/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
ruby中的singleton类到底是什么?_Ruby_Oop_Singleton - Fatal编程技术网

ruby中的singleton类到底是什么?

ruby中的singleton类到底是什么?,ruby,oop,singleton,Ruby,Oop,Singleton,Ruby中的单例类本身就是一个类吗?这就是为什么所有对象都属于“类”的原因吗?这个概念是模糊的,但我相信这与为什么我可以定义一个类方法有关(class foo;def foo.bar…) Ruby中的singleton类是什么?最实用的/操作或定义方式(IMHO)是:作为继承链,或方法查找/解析顺序。这张照片可能会有帮助 这是R1.9,对比了内置类和用户定义类:我还在消化这个 此外,我认为“单例对象”一词的用法令人困惑,这是一个不同的概念。单例对象来自一个类,该类重写了其构造函数/实例化器方

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


Ruby中的singleton类是什么?

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

这是R1.9,对比了内置类和用户定义类:我还在消化这个


此外,我认为“单例对象”一词的用法令人困惑,这是一个不同的概念。单例对象来自一个类,该类重写了其构造函数/实例化器方法,因此您只能分配该类中的一个。

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

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 singleton\u class=(class=)打开对象的singleton类#
irb(主):013:0>singleton_class.method_defined?:method1
=>正确
irb(主):014:0>singleton_class.method_defined?:method2
=>正确
irb(main):015:0>other_singleton_class=(类#
irb(主):016:0>其他\u单例\u类。方法\u定义?:方法1
=>正确
irb(主):017:0>其他\u单例\u类。方法\u定义?:方法2
=>错误
因此,向对象添加单例方法的另一种方法是在对象的单例类打开的情况下定义它们:

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
irb(主):018:0>零级
irb(主):019:0>foo.method3
3.
=>零
irb(主):022:0>Foo.method_定义?:method3
=>错误
总之:

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

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

    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

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

作为对@Pistos answer的更新,从1.9.2版Ruby为获取Singleton类添加了新语法

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

< P>一个简单的术语中的单类是一个特殊的类Ruby鞭,它是在单个对象上定义的宿主方法。在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>)

虽然这个答案已经有一年多的历史了,而且这个链接很有帮助,但是如果你把答案的重要部分发布在这里,或者你的帖子有被删除的风险,那就更好了。如果你愿意,你仍然可以包含这个链接,但只是作为一个“参考”。答案应该独立存在,而不需要链接。同意@bluefeet here谢谢@bluefeet,更新了回复以回应你的评论。在我的墓碑上写着“RIP Ruby Singleton.Pistos拯救了我的理智。”@sawa我很欣赏你编辑的意图,但我觉得它们对我帖子的意义和交流有点太大的改变,所以我已经将你的编辑回滚了。其中一个链接已经死了。另一个是日语!
    foo.singleton_methods  # => [:size]
    bar.singleton_methods  # => []
 singleton_class = ( class << foo; self; end )
singleton_class = foo.singleton_class
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>)
user.singleton_class # #<Class:#<User:0x0000559c66b47c58>>
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