Entries RSS Comments RSS

Redefining the “+” method in Ruby

This is still one of my favorite things to do on a rainy day:

class Fixnum
  def + (x)
    self - x
  end
end

1 + 1
=> 0

Don’t forget to redefine the + sign for floats as well! 1.3 + 2.1 would still equal 3.4, unless you do the same thing for class Float. :)

Tags:

Leave a Reply