我有這個模塊:爲什麼`Object.include`和`Fixnum.prepend`的順序很重要?
module MyMod
def +(other)
puts "hello"
end
end
這個成功覆蓋+
爲Fixnum
:
Fixnum.prepend(MyMod)
123 + :test # outputs "hello"
比方說,我需要的+
運營商覆蓋了Fixnum
和其他對象。這成功地將覆蓋+
爲Fixnum
和其他對象:
Fixnum.prepend(MyMod)
Object.include(MyMod)
123 + :test # outputs "hello"
但是,如果我改變prepend
和include
的順序,我的覆蓋沒有影響:
Object.include(MyMod)
Fixnum.prepend(MyMod)
123 + :test # error: -:10:in `+': :test can't be coerced into Fixnum (TypeError)
爲什麼的include
順序和prepend
這裏有這個效果嗎?
哦,順便說一句,這是我用它爲:https://gist.github.com/henrik/36b991205d6c772b5a1f#file-pipeline1-rb –