重新定义 Float#/
似乎没有效果:
class Float
def /(other)
"magic!"
end
end
puts 10.0/2.0 # => 5.0
但是当另一个中缀运算符 Float#*
被重新定义时,Float#/
突然采用了新的定义:
class Float
def /(other)
"magic!"
end
def *(other)
"spooky"
end
end
puts 10.0/2.0 # => "magic!"
我很想知道是否有人可以解释这种行为的来源,以及其他人是否得到相同的结果。
- ruby : ruby 2.0.0p353 (2013-11-22) [x64-mingw32]
要快速确认错误,请运行 this script .
最佳答案
关于ruby - 猴子修补 float 中缀运算符产生意想不到的结果,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20501203/