重新定义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]
要快速确认错误,请运行此脚本.