Ruby 有谁知道 Ruby 设计 Singleton methods 的原因

ibachue · 2013年03月27日 · 最后由 fsword 回复于 2013年03月28日 · 5016 次阅读

Hi all,

这个问题是我同事问我的,可惜我只知道 Singleton methods 这套模型的实现以及如何使用 Singleton methods,对于为什么 Ruby 要设计有这个功能我就完全不了解了。

大家谁能帮助我一下,谢谢。

这样不就是对象个性化了吗,更符合现实世界。

#1 楼 @chenge 这个理由额。。我同事未必接受。。

那我打个电话,问问 Matz 吧。

这个问题这么简单,随便想一下就明白了

#3 楼 @chenge Matz 在和我吃饭,等等再打。

6 楼 已删除

Singleton methods 不是 Ruby 实现的模型,这个模式在面向对象编程的时候就有了,在《设计模式》里面被归类命名。

Ruby 的语法可以很容易实现 Singleton methods,要做什么看你自己了。或者你可以看看《设计模式》(Java)这本书,还有一本《Ruby 设计模式》。

我的看法,觉得怎么写好就怎么写,模仿别人的写法是捷径,我几乎不去想这是什么模式那是什么模式。

#7 楼 @Rei 你说的不会是单例模式吧。。我说的不是那个。。。

#5 楼 @zgm 吃寿司嘛?233

#8 楼 @iBachue 是,我搞错了。

我自己几乎没写过 Singleton methods,搜了一下

http://www.rubyist.net/~slagell/ruby/singletonmethods.html

Singleton methods are often used for elements of a graphic user interface (GUI), where different actions need to be taken when different buttons are pressed.

归根结底在于一切皆对象,本质上来说没有类方法和实例方法,只有属于对象的方法。

#11 楼 @Rei 额 那这样也用不到 singleton methods 啊 给 button 的 press 事件设置一个 callback 即可

还有 你不可能没用过 singleton methods 的,我不相信你没写过这种代码

class A
  def self.f(...) ... end
end

class A
  class << self
    def f(...) ... end
  end
end

#13 楼 @iBachue 奥,原来这也叫 singleton methods 啊,我对这些没什么概念。

#12 楼 @kenshin54 对哦 这个说不定是个有力的理由

#13 楼 @iBachue 你这个例子就是类方法,用的 eignclass。

#14 楼 @Rei 显然是的 否则你说 A 这个类对象凭什么可以有 f 这个方法呢?人家 B 也是类对象,就没有这个方法的 所以肯定是 singleton method 啦

@Rei 所言,Singleton 是面向对象语言的基本特性,非 Ruby 首创。Singleton 在基于原型编程(Prototype-based programming)的思路中得到了充分的应用,详见维基百科 http://zh.wikipedia.org/zh-cn/%E5%8E%9F%E5%9E%8B%E7%A8%8B%E5%BC%8F%E8%A8%AD%E8%A8%88

#16 楼 @chenge 嗯 这两个词在 Ruby 中同义

#18 楼 @swordray 嗯 这个可能也是理由之一 不过 Ruby 并非一门面向原型的语言。类似于

// Example of true prototypal inheritance style 
// in JavaScript.

// "ex nihilo" object creation using the literal 
// object notation {}.
var foo = {name: "foo", one: 1, two: 2};

// Another "ex nihilo" object.
var bar = {three: 3};

// Gecko and Webkit JavaScript engines can directly 
// manipulate the internal prototype link.
// For the sake of simplicity, let us pretend 
// that the following line works regardless of the 
// engine used:
bar.__proto__ = foo; // foo is now the prototype of bar.

// If we try to access foo's properties from bar 
// from now on, we'll succeed. 
bar.one // Resolves to 1.

// The child object's properties are also accessible.
bar.three // Resolves to 3.

// Own properties shadow prototype properties
bar.name = "bar";
foo.name; // unaffected, resolves to "foo"
bar.name; // Resolves to "bar"

这样的代码在 Ruby 中无法实现。

简单来说就是让所有方法都有接收者,反例看 java

#20 楼 @iBachue Ruby 是面向对象语言(Object-Oriented Language),基于原型编程(Prototype-Based Programming)只是 Ruby 的一种使用方式。Ruby 确实不能修改 Object.new.singleton_class 本身,而我也没有说过 Ruby 是“面向原型”的。

其实,从来就没有singleton XXX,只是叫的人多了,它才是singleton XXX

你需要直接操作实例,但是因为 Ruby 里封装的概念,你在 Ruby 里访问a.b的时候,其实是调用了那个方法。所以,需要引入singleton class来直接操作实例。

另外一个是 Ruby 没有定义函数的概念。本质上设定singleton method其实类似就是a.b = function() {}这样一个操作。而因为 Ruby 里,你只能def方法,而不能def函数,同时因为上一条,所以要引入专门的语法,来完成给一个实例设置一个值为方法的属性这件事...

就相当于 java 的静态方法

#24 楼 @fleuria 你看错了吧,静态方法那是 class method。

#24 楼 @fleuria 哈哈,同一个坑里

你说的可是《松本行弘的程序世界》这本书的 P92、93 这段内容?

这要从 ruby 的对象模型讲起,理解类继承,理解 eigen class

我强烈倾向于使用 eigen class 这个名词以避免混淆,至于为什么需要设计它,可能需要写篇 blog 来说明了

需要 登录 后方可回复, 如果你还没有账号请 注册新账号