1. 程式人生 > >Ruby中的反射(Reflection)-通過類名稱構造類物件

Ruby中的反射(Reflection)-通過類名稱構造類物件

在Java語言中,提供了發射機制,通過發射機制可以通過字串構造出這個物件,可以獲取物件的所有方法(包括私有方法),可以呼叫私有方法,可以更改成員變數的值(包括私有的成員變數)。
Ruby也是面向物件的高階語言,當然也提供了反射機制,今天我們討論通過類名稱構造類物件的功能。

我們先看普通的構造:

module ModuleA
#the class name, later we will use it to create the corresponding object
CLASS_NAME_OF_WOOD = "ModuleA::Wood"
CLASS_NAME_OF_WOODDESK = "ModuleA::WoodDesk"
CLASS_NAME_OF_WOODCHAIR = "ModuleA::WoodChair"

class Wood
def initialize
@desc = "I am a primal wood"
end

def say
puts @desc
end
end

class WoodDesk < Wood def initialize @desc = "I am a desk made of wood" end def say_private puts "actually, i have some bug but no public" end public :say private :say_private end class WoodChair < Wood def initialize @desc = "I am a chair made of wood" end def say_private puts "I Want get married with a WoodDesk..." end def smile puts "ha hah hah haha ...." end public :say private :say_private, :smile end end
定義了一個基礎類Wood,有兩個子類:WoodDesk, WoodChair,子類有分別有一個私有方法 say_private。

我們new出物件來執行:

#the normal initailze
wood = ModuleA::Wood.new
wood.say
desk = ModuleA::WoodDesk.new
desk.say
chair = ModuleA::WoodChair.new
chair.say

#try call the private method
puts "desk respond to say_private? #{desk.respond_to? :say_private}"
desk.say_private if desk.respond_to? :say_private

上面程式碼,執行public方法say,然後嘗試執行private方法 say_private,執行先check是否能夠執行,返回結果是不能執行,desk.respond_to? :say_private返回false:

I am a primal wood
I am a desk made of wood
I am a chair made of wood
desk respond to say_private? false

好,現在我們通過反射機制來構造物件,並嘗試執行其私有方法。

我們注意到模組的定義中有三個常量,定義的是類名稱,

#the class name, later we will use it to create the corresponding object
CLASS_NAME_OF_WOOD = "ModuleA::Wood"
CLASS_NAME_OF_WOODDESK = "ModuleA::WoodDesk"
CLASS_NAME_OF_WOODCHAIR = "ModuleA::WoodChair"

下面會通過這三個變數來理解Module.constants方法。

下面程式碼片段,基於上面的類定義:

#get all module constants
obj_list = Array.new
tmp_const_sym_list = ModuleA.constants
tmp_const_sym_list.each do | sym |
obj_list << ModuleA.const_get(sym) puts "calss = #{sym.class}, value = #{sym}" end

我們注意到 ModuleA.constants,這個方法是Module模組中的,其作用是返回模組中所有常量的Symbol物件。我們看結果輸出:

calss = Symbol, value = CLASS_NAME_OF_WOOD
calss = Symbol, value = CLASS_NAME_OF_WOODDESK
calss = Symbol, value = CLASS_NAME_OF_WOODCHAIR
calss = Symbol, value = Wood
calss = Symbol, value = WoodDesk
calss = Symbol, value = WoodChair

從結果中看到,定義的三個常量和類名稱都被返回了。所以注意:Ruby中的常量是包含定義的常量(變數)和類名稱,注意他們都是Symbol物件。

不過我們是需要根據類名稱構造類物件,那麼那三個常量就是沒用的,需要刪除。我們通過正則表示式匹配名字,來過濾。上面的程式碼修改一下:

#get all module constants
sym_list = Array.new
tmp_const_sym_list = ModuleA.constants
tmp_const_sym_list.each do | sym |
puts "calss = #{sym.class}, value = #{sym}"
sym_list << ModuleA.const_get(sym) if /^Wood\w*/ =~ sym.to_s end

sym_list << ModuleA.const_get(sym) if /^Wood\w*/ =~ sym.to_s,僅儲存以Wood開頭的symbol,這樣我們就過濾掉了那三個常量。 找都類名稱之後,開始構造物件:
#create object from symbol
obj_list = Array.new
sym_list.each do | sym |
obj = sym.new
obj_list << obj puts "create the object: #{obj}" end begin obj_list.each do | wood | wood.say end

呼叫Symbol的new方法構造出次物件(sym.new),然後我們呼叫物件的say方法:

create the object: #<:wood:0x007fb10284c500>
create the object: #<:wooddesk:0x007fb10284c1b8>
create the object: #<:woodchair:0x007fb10284c0f0>
I am a primal wood
I am a desk made of wood
I am a chair made of wood

達到了我們預期的結果。