# Overloading math operations

**URL:** <https://www.ruby-forum.com/t/overloading-math-operations/59765>\
**Category:** Ruby\
**Created:** [May 6, 2006, 7:56am UTC](https://www.ruby-forum.com/t/overloading-math-operations/59765 "2006-05-06T07:56:57Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![0ffh](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/0/ea666f/32.png) [@0ffh](https://www.ruby-forum.com/u/0ffh)\
**Post date:** [May 6, 2006, 7:56am UTC](https://www.ruby-forum.com/t/overloading-math-operations/59765/1 "2006-05-06T07:56:57Z")

</div>

Hija,

been makin a little “Vector” class by extending Array.  
Now I wanna add Vector support to the native numeric  
data types in ruby. Means: Not only Vector+Numeric  
but also Numeric+Vector should call my + function.

But I have a syntax prolem… ☹

Example (not working!):

class Fixnum  
alias old+ + # here i have the syntax problem  
def +(other)  
if (other.class\<=Vector)  
other+self  
else  
self.old+(other)  
end  
end  
end

Someone please help poor ruby nooby! :-)))

Regards, 0ffh

---

<div class="post-metadata">

**Author:** ![0ffh](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/0/ea666f/32.png) [@0ffh](https://www.ruby-forum.com/u/0ffh)\
**Post date:** [May 6, 2006, 8:14am UTC](https://www.ruby-forum.com/t/overloading-math-operations/59765/2 "2006-05-06T08:14:17Z")

</div>

Use this alias line…

alias :old\_plus :+

both of the arguments to alias need to be symbols. also, “old+” wont be  
a  
happy method name.

Mark

---

<div class="post-metadata">

**Author:** ![0ffh](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/0/ea666f/32.png) [@0ffh](https://www.ruby-forum.com/u/0ffh)\
**Post date:** [May 6, 2006, 8:21am UTC](https://www.ruby-forum.com/t/overloading-math-operations/59765/3 "2006-05-06T08:21:52Z")

</div>

Mark Van H. wrote:

> alias :old\_plus :+  
> both of the arguments to alias need to be symbols.  
> also, “old+” wont be a happy method name.

Thanks alot!

Actually I have already tried “alias :old+ :+”,  
which gave a syntax error as well, but I didn’t  
realise it was yet another one… 😉

Regards, 0ffh

---

<div class="post-metadata">

**Author:** ![0ffh](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/0/ea666f/32.png) [@0ffh](https://www.ruby-forum.com/u/0ffh)\
**Post date:** [May 6, 2006, 8:44am UTC](https://www.ruby-forum.com/t/overloading-math-operations/59765/4 "2006-05-06T08:44:23Z")

</div>

2006/5/6, Frank H. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid):

> realise it was yet another one… 😉  
> If you’re not doing it for the fun of it: there are already vector and  
> matrix classes for Ruby plus libraries for manipulating them like  
> narray (see the RAA).

Kind regards

robert

---

<div class="post-metadata">

**Author:** ![0ffh](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/0/ea666f/32.png) [@0ffh](https://www.ruby-forum.com/u/0ffh)\
**Post date:** [May 6, 2006, 8:58am UTC](https://www.ruby-forum.com/t/overloading-math-operations/59765/5 "2006-05-06T08:58:55Z")

</div>

Robert K. wrote:

> If you’re not doing it for the fun of it: there are already vector and  
> matrix classes for Ruby plus libraries for manipulating them like  
> narray (see the RAA).

I have seen and downloaded narray.

I don’t use it because it is usually more hassle to  
get stuff like that running and learn how to use it  
and then find out that it is not quite what you’ve  
been looking for after all than just DIY…

I am sitting here in a Hotel room with a newbought,  
virgin Windoze notebook. I don’t even want to try  
to estimate how long it will take me to get the  
compiler, utilities and whatnot up and running to  
even _start_ to compile narray.  
But if I can get no-hassle installable binaries  
somewhere, I’ll be happy to give it a chance.

Regards, Frank

---

<div class="post-metadata">

**Author:** ![0ffh](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/0/ea666f/32.png) [@0ffh](https://www.ruby-forum.com/u/0ffh)\
**Post date:** [May 6, 2006, 9:28am UTC](https://www.ruby-forum.com/t/overloading-math-operations/59765/6 "2006-05-06T09:28:14Z")

</div>

Eli B. wrote:

> Perhaps a parallel for ActiveState’s PPM (pre compiled modules) would be  
> nice. Unfortunately, Ruby still has no ActiveState version. Anyone knows  
> about plans to make it ?

No idea, but it makes for a very attractive idea!

Reagrds, 0ffh

---

<div class="post-metadata">

**Author:** ![0ffh](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/0/ea666f/32.png) [@0ffh](https://www.ruby-forum.com/u/0ffh)\
**Post date:** [May 6, 2006, 9:06am UTC](https://www.ruby-forum.com/t/overloading-math-operations/59765/7 "2006-05-06T09:06:09Z")

</div>

Frank H. wrote:

> I am sitting here in a Hotel room with a newbought,  
> virgin Windoze notebook. I don’t even want to try  
> to estimate how long it will take me to get the  
> compiler, utilities and whatnot up and running to  
> even _start_ to compile narray.  
> But if I can get no-hassle installable binaries  
> somewhere, I’ll be happy to give it a chance.
> 
> Regards, Frank

Ah… the reason that made my avoid so many Perl modules in my time… I  
guess it’s the same for Ruby. Perhaps it should be made a custom that  
every extension library has a pure-Ruby version. This is useful for  
prototyping - if one sees that the library fits his needs, he can later  
put in the effort to actually compile and bind the extension to his  
Ruby.

Perhaps a parallel for ActiveState’s PPM (pre compiled modules) would be  
nice. Unfortunately, Ruby still has no ActiveState version. Anyone knows  
about plans to make it ?
