Overloading math operations


#1

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… :frowning:

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


#2

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


#3

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… :wink:

Regards, 0ffh


#4

2006/5/6, Frank H. removed_email_address@domain.invalid:

realise it was yet another one… :wink:
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


#5

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


#6

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


#7

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 ?