13
我一直在嘗試修補全局緩存模塊,但我無法弄清楚爲什麼這不起作用。alias_method和class_methods不混合?
有沒有人有任何建議?
這是錯誤:
NameError: undefined method `get' for module `Cache'
from (irb):21:in `alias_method'
...這段代碼生成:
module Cache
def self.get
puts "original"
end
end
module Cache
def self.get_modified
puts "New get"
end
end
def peek_a_boo
Cache.module_eval do
# make :get_not_modified
alias_method :get_not_modified, :get
alias_method :get, :get_modified
end
Cache.get
Cache.module_eval do
alias_method :get, :get_not_modified
end
end
# test first round
peek_a_boo
# test second round
peek_a_boo
你並不需要整個'Cache.module_eval做類<
Chuck
2010-05-27 21:55:33
@抓,好點;更新! – molf 2010-05-27 22:00:04