Is there a ruby equivelent for __rmul__ in ruby? I can't seem to find anything about this.
Asked
Active
Viewed 161 times
3 Answers
2
not sure about py, but is overloading operator *
what you want?
if then, a method *
will be fine.
a=Object.new
def a.*(rhs); rhs+2;end
a*2 #=> 4

Jokester
- 5,501
- 3
- 31
- 39
-
2rmul is slightly different, it is because you can have like 2*object OR object*2 I need somethign that runs on 2*object not object*2 – KatGaea Jun 20 '12 at 16:16
-
in ruby, `2*object` is `like 2.*(object)`, and `object*2` is `object.*(2)`. so there is some difference, but not that different – Jokester Jun 20 '12 at 16:23
-
What would it do if instead of 2, it was some other object, also with an overloaded * ? – KatGaea Jun 20 '12 at 16:28
-
to be more general, `a.*(b)` for `a*b` then. – Jokester Jun 20 '12 at 16:34
-
I have posted the answer that seems to work fine for me now, but thankyou very much for the answer – KatGaea Jun 20 '12 at 16:35
-
glad if it helped maybe a little :) – Jokester Jun 20 '12 at 16:38
0
Looking into things further, it seems the equivelent for rmul is to add
def coerce(other)
return self, other
end
then have the usual * overloaded handle other types too with is_a?

KatGaea
- 996
- 1
- 12
- 31
0
Check out this discussion here about coerce
in Ruby: