Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Existence of right addition/multiplication in ruby?

Tags:

ruby

I've seen how to overload + and * in Ruby, so that my_obj + other calls my_obj.+(other). In Python, you do this with __add__, and there's a corresponding __radd__ for overloading other + my_obj. Is there really no equivalent right-sided addition/multiplication in Ruby, and does that make it necessary to redefine + for each potential class of other?

In brief: say I have an object X which belongs to some new class defined by me. It's easy to write code for X + 5, but it seems that in order to handle 5 + X I'd need to redefine Fixnum.+. Is this true?

like image 265
Peter Avatar asked Aug 16 '09 10:08

Peter


1 Answers

No, you don't need to redefine Fixnum#+ (or any other arithmetic method of ruby's numeric classes). The arithmetic methods of the numeric classes will call coerce if the two operands are not the same class. So if you define a coerce method for your class, 5 + instance_of_your_class will work fine without any changes to Fixnum#+.

Edit: Here's an example of using coerce:

class MyNum
  attr_accessor :num

  def initialize(num)
    @num = num
  end

  def +(o)
    lhs, rhs = coerce(o)
    MyNum.new(lhs.num + rhs.num)
  end

  def coerce(o)
    if o.is_a? MyNum
      [self, o]
    else
      [self, MyNum.new(o)]
    end
  end
end

MyNum.new(5)+3 #=> #<MyNum:0x87fca08 @num=8>
3+MyNum.new(5) #=> #<MyNum:0x8807980 @num=8>
like image 183
sepp2k Avatar answered Oct 30 '22 14:10

sepp2k