From: Nokan Emiro Date: 2013-01-17T02:15:23+09:00 Subject: commutative operations --f46d04462c3854152604d36aecfb Content-Type: text/plain; charset=UTF-8 Hi there, How would you solve the issue below? I'm looking for an elegant solution for this one. I usually bump into this when I create my own types. I have a class that extends Ruby's standard type set with my own type. This new type is something that is supposed to be able to work together with the already existing, built-in classes as close as possible. I mean it is a valid thing to for instance add a Fixnum to it, multiply it by a Rational number (which isn't zero, of course), etc. The class can do this, because it's :+, :*, :/, :- methods are aware of these types. So, if x is an instance of my class, x + 1 works fine, just like x * 2, and all the others. The problem is that while I write programs, I always think about + as a commutative operation, so I unconsciously expect that not only x + 1, but the expression 1 + x gives the same result, which is of course not true, because 1 + x calls Fixnum#+, and that method does not know anything about my class. How could I make at least Numeric classes behave this way? What I want is that if a Numeric instance method receives an object as a parameter that is an instance of my class, it should call the same method on the argument with itself (I mean 'self') as the parameter in case of commutative operatins, like + and *. The problem is a bit more difficult in case of order-dependant operators, like / and -... --f46d04462c3854152604d36aecfb Content-Type: text/html; charset=UTF-8 Content-Transfer-Encoding: quoted-printable Hi there,

How would you solve the issue below? =C2=A0I&#= 39;m looking for an elegant
solution for this one. =C2=A0I usuall= y bump into this when I create my own
types.

=
I have a class that extends Ruby's standard type set with my own
type. =C2=A0This new type is something that is supposed to be able to<= /div>
work together with the already existing, built-in classes as clos= e as
possible. =C2=A0I mean it is a valid thing to for instance a= dd a Fixnum
to it, multiply=C2=A0it by a Rational number (which isn't zero, of= course), etc.
The class can do this, because it's :+, :*, :/= , :- methods are
aware of these types. =C2=A0So, if x is an insta= nce of my class, x + 1 works
fine, just like x * 2, and all the others.

Th= e problem is that while I write programs, I always think about + as
a commutative operation, so I unconsciously expect that not only
x + 1, but the expression 1 + x gives the same result, which is of
course not true, because 1 + x calls Fixnum#+, and that method
=
does not know anything about my class.

How co= uld I make at least Numeric classes behave this way? =C2=A0What
I want is that if a Numeric instance method receives an object as a
parameter that is an instance of my class, it should call the same<= /div>
method on the argument with itself (I mean 'self') as the= parameter
in case of commutative operatins, like + and *. =C2=A0The problem is a=
bit more difficult in case of order-dependant operators, like / = and -...

--f46d04462c3854152604d36aecfb--