我有这个模块:为什么`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 –