class Foo
def self.one; 1 end
class << self
def two; 2 end
end
end
puts Foo.singleton_methods.inspect # => ["two", "one"]
I've been told the above methods "one" and "two" are conceptually different but I don't see how. They are both singleton methods - what's the difference in concept and also application?
我被告知上面的方法“一”和“二”在概念上是不同的,但是我不知道是怎么回事。它们都是单例方法——概念和应用有什么不同?
2 个解决方案
#1
7
In application, there is no difference. In concept, the difference is subtle, but in the first case, you are operating in the current context, and defining a method on another class instance (actually, an instance method in its Eigenclass), whereas in the second case, you are entering the context of the the metaclass ("Eigenclass") of other class instance, and then defining an instance method.
在应用上,没有区别。在概念上,这两者的区别是微妙的,但在第一种情况下,你当前上下文中的操作,定义一个方法在另一个类实例(实际上,实例方法Eigenclass),而在第二种情况下,你正在进入的背景下的元类(“Eigenclass”)其他类实例,然后定义一个实例方法。
Edit:
编辑:
I should add that the reasons for choosing the class << self
in some cases are...
我要补充的是,在某些情况下选择类< self的原因是……
- Cleaner syntax when defining more than a few class-methods.
- 定义多个类方法时使用更简洁的语法。
- You can execute other kinds of code in the Eigenclass context besides just
def my_method ...
. You can, for instance, sayattr_accessor :some_attribute
in that block of code. - 你可以在Eigenclass上下文中执行其他类型的代码除了def my_method ....例如,可以在该代码块中写入attr_accessor:some_attribute。
#2
4
I strongly recommend you to read "Metaprogramming Ruby". This book explains about Ruby's object model, including singleton method and singleton class.
我强烈建议您阅读“元编程Ruby”。这本书解释了Ruby的对象模型,包括单例方法和单例类。
http://pragprog.com/titles/ppmetr/metaprogramming-ruby
http://pragprog.com/titles/ppmetr/metaprogramming-ruby
This article also explains same topic.
本文还解释了相同的主题。
http://www.contextualdevelopment.com/articles/2008/ruby-singleton
http://www.contextualdevelopment.com/articles/2008/ruby-singleton
#1
7
In application, there is no difference. In concept, the difference is subtle, but in the first case, you are operating in the current context, and defining a method on another class instance (actually, an instance method in its Eigenclass), whereas in the second case, you are entering the context of the the metaclass ("Eigenclass") of other class instance, and then defining an instance method.
在应用上,没有区别。在概念上,这两者的区别是微妙的,但在第一种情况下,你当前上下文中的操作,定义一个方法在另一个类实例(实际上,实例方法Eigenclass),而在第二种情况下,你正在进入的背景下的元类(“Eigenclass”)其他类实例,然后定义一个实例方法。
Edit:
编辑:
I should add that the reasons for choosing the class << self
in some cases are...
我要补充的是,在某些情况下选择类< self的原因是……
- Cleaner syntax when defining more than a few class-methods.
- 定义多个类方法时使用更简洁的语法。
- You can execute other kinds of code in the Eigenclass context besides just
def my_method ...
. You can, for instance, sayattr_accessor :some_attribute
in that block of code. - 你可以在Eigenclass上下文中执行其他类型的代码除了def my_method ....例如,可以在该代码块中写入attr_accessor:some_attribute。
#2
4
I strongly recommend you to read "Metaprogramming Ruby". This book explains about Ruby's object model, including singleton method and singleton class.
我强烈建议您阅读“元编程Ruby”。这本书解释了Ruby的对象模型,包括单例方法和单例类。
http://pragprog.com/titles/ppmetr/metaprogramming-ruby
http://pragprog.com/titles/ppmetr/metaprogramming-ruby
This article also explains same topic.
本文还解释了相同的主题。
http://www.contextualdevelopment.com/articles/2008/ruby-singleton
http://www.contextualdevelopment.com/articles/2008/ruby-singleton