Question

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.

Was it helpful?

Solution

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

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top