From: Robert Klemme Date: 2010-10-01T23:06:40+09:00 Subject: Re: Operator Overload Please do not top post. On Fri, Oct 1, 2010 at 3:09 PM, Adriano Ferreira wrote: > Robert, I get the point. However it's important to notice that there's no such > thing as operator overloading in Ruby. Things like :+, :-, :*, etc, are just > plain methods. Adriano, operator "+" and all others are certainly overloaded (as in many programming languages btw) because different implementations get called depending on the left hand type. Method #coerce is just a way to get double dispatch working. > You can achieve what you want by opening the Fixnum class and I do not want something in particular (at least for the sake of this thread) so I am not sure what you refer to. > redefining the method :+. Important to say is that all instances of this will be > affected as soon as you make the change. This is precisely the reason to properly implement operators and #coerce in new classes that should play nicely with existing classes. > Eg: > > class Fixnum >  def -(x) >    self + x >  end > end > > puts 1 - 1 # => 2 This is certainly not something I would want. > Another tip: In Ruby, every expression returns a value, so there's no need to > explicit the 'return'. Yes, I know. As far as I can see in my blog article there is no "return" in the code. Cheers robert -- remember.guy do |as, often| as.you_can - without end http://blog.rubybestpractices.com/