Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Monkey Patching Float Infix Operator's Produces Unexpected Results

Redefining Float#/ appears to have no effect:

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

But when another infix operator Float#* is redefined, Float#/ suddenly takes on the new definition:

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

I would love to hear if anybody can explain the source of this behavior and if anybody else gets the same results.

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

To quickly confirm the bug, run this script.

like image 532
ChrisTimps Avatar asked Dec 10 '13 17:12

ChrisTimps


1 Answers

This appears to be a bug in the implementation of Ruby. A bug report has been filed here.

In the mean time, you may either switch implementations or switch versions. 1.8.7 appears to be bug free.

EDIT

This bug was fixed with revision 44127

like image 172
Dan Grahn Avatar answered Nov 09 '22 22:11

Dan Grahn