備注
如果說哪門語言對我影響最大,那就是Ruby了,在.NET陣營多年,試圖去用C#的思維去解釋很多東西,當然解釋Java是足夠了,可惜我也用了好幾年去解釋Javascript,結果是可想而知的:解釋不通。直到我遇到Ruby,這讓我了解到一件事:不能用一種語言的思維去解釋另外一種語言,當然某些局部特點是有可比性的。
本文重點解釋一下Ruby的對象模型(可以理解我Javascript的原型模型),有此可以看出Ruby的完美和統一。
對象模型
無圖無真相
基本規則
- 一切皆為對象:instance是一般對象、#instance是臨時類型對象、Child是一般類型對象、Class是特殊的元類型對象(其實例是一般類型對象)、Mixinable是一般模塊對象、Module是元模塊對象(其實例是一般模塊對象)。
- 只有實例方法,即方法只能定義在類型上(不像Javascript,可以存儲在對象上),即:如果 xxx.class = Class or xxx.class = Module 那么 xxx 可以定義實例方法。
- 所有實例變量只存儲在對象自身上(不像Javascript,可以存儲在原型鏈的任何位置)。
- 所有類型都可以在任何時候進行修改(Open Class)。
- instance.method的查找規則:先查看singleton class,然后遞歸遍歷所有super class和Mixed Module,遇到第一個即返回,這個過程也叫:右移一步,然后向上。
代碼示例
1 # coding: utf-8 2 3 class Parent 4 def hi() 5 puts "hi" 6 end 7 end 8 9 module Mixinable 10 def hey() 11 puts "hey" 12 end 13 end 14 15 class Child < Parent 16 include Mixinable 17 18 def hello() 19 puts "hello" 20 end 21 end 22 23 instance = Child.new 24 instance.hello 25 instance.hey 26 instance.hi
如何修改Singleton Class?
第一種方式
1 class << instance 2 def instance_singleton_method_one 3 puts "instance_singleton_method_one" 4 end 5 end 6 7 instance.instance_singleton_method_one
第二種形式
1 def instance.instance_singleton_method_two 2 puts "instance_singleton_method_two" 3 end 4 5 instance.instance_singleton_method_two
如何修改類型,如Child?
注意:下面演示的是“Open Class”,不是重新定義一個類型。
1 class Child 2 def child_method_one() 3 puts "child_method_one" 4 end 5 end 6 7 instance.child_method_one
類型方法是特殊的實例方法,這些方法定義在類型的Singleton Class中。
第一種方式
1 class Child 2 def Child.child_class_method_one() 3 puts "child_class_method_one" 4 end 5 end 6 7 Child.child_class_method_one
第二種形式
1 class Child 2 def self.child_class_method_two() 3 puts "child_class_method_two" 4 end 5 end 6 7 Child.child_class_method_two
第三種形式
1 def Child.child_class_method_three 2 puts "child_class_method_three" 3 end 4 5 Child.child_class_method_three
第四種形式
1 class << Child 2 def child_class_method_four() 3 puts "child_class_method_four" 4 end 5 end 6 7 Child.child_class_method_four
備注
圖中很多關系有興趣的朋友可以自己驗證,本文沒有涉及元編程,元編程不過是按照一定的元數據來修改類型定義或生成類型定義,也就是說元編程的前提是類型可以動態的修改,了解了本文,元編程不在話下。