Monkey Patching Float Infix Operator Gives Unexpected Results

Overriding Float#/ does not seem to have an effect:

 class Float def /(other) "magic!" end end puts 10.0/2.0 # => 5.0 

But when another infix operator Float#* redefined, Float#/ unexpectedly accepts a new definition:

 class Float def /(other) "magic!" end def *(other) "spooky" end end puts 10.0/2.0 # => "magic!" 

I would like to hear if someone can explain the source of this behavior, and if someone else gets the same results.

  • Ruby: ruby ​​2.0.0p353 (2013-11-22) [x64-mingw32]

To quickly confirm the error, run this script .

+6
source share
1 answer

This seems to be a bug in the implementation of Ruby. An error report has been sent here .

At the same time, you can either switch versions or switch versions. 1.8.7 does not seem to contain errors.

EDIT

This bug has been fixed with revision 44127

+3
source

All Articles