2011-02-04 35 views
3

紅寶石1.8.7(2010-12-23 patchlevel 330)[i686-darwin10.5.0]重寫is_a的正確方法是什麼?和kind_of?

我錯過了什麼嗎?

module Mixin 
    def is_a?(o) 
    return false if o == Hash 
    super 
    end 
    alias_method :kind_of?, :is_a? 
end 

class Doc < Hash 
    include Mixin 
end 

puts Doc.new().is_a?(Doc) # => true 
puts Doc.new().kind_of?(Doc) # => super: no superclass method `is_a?' 

預計:

puts Doc.new().is_a?(Doc) # => true 
puts Doc.new().kind_of?(Doc) # => true 
puts Doc.new().is_a?(Hash) # => false 
puts Doc.new().kind_of?(Hash) # => false 

一切都是Rubinius和紅寶石1.9.2p136

+0

也許它會在一分鐘內打到我,但如果你能解釋你期望看到的並指出與之不同的部分,這將會有所幫助。我沒有看到你的Mixin對你正在測試的代碼有什麼影響,或者你的自定義`Doc2#is_a?`方法有什麼不同。你永遠不會傳入哈希! – Phrogz 2011-02-04 04:39:01

回答

相關問題