Entries RSS Comments RSS

Posts Tagged ‘ruby is cute’

Redefining the “+” method in Ruby

Friday, January 15th, 2010

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. :)