Help overloading Fixnum#+

Hello,

I’m trying to overload Fixnum#+, but I am getting what seems to be a
recursion (see below). Does anyone know what that happens? Perhaps I
don’t know how “alias” works.

$ cat >test.rb
class Fixnum
alias plus +
def +(other)
puts “hello”
self.plus(other)
end
end
$ irb

require ‘test’
=> true
2+2
hello
hello
hello
hello
hello
hello
hello
hello
hello
hello
hello
hello
hello
hello
hello
hello
hello
hello
hello
hello
hello
=> 4

···


Daniel Carrera
Graduate Teaching Assistant. Math Dept.
University of Maryland. (301) 405-5137

Try
  puts "Adding #{self} to #{other}"

to see other additions going on in irb :slight_smile: It doesn't do this if you run it
as a standalone script.

Regards,

Brian.

···

On Sun, Feb 09, 2003 at 09:14:23AM +0900, Daniel Carrera wrote:

I'm trying to overload Fixnum#+, but I am getting what seems to be a
recursion (see below). Does anyone know what that happens? Perhaps I
don't know how "alias" works.

$ cat >test.rb
class Fixnum
    alias plus +
    def +(other)
        puts "hello"
        self.plus(other)
    end
end