From: Henning von Rosen Date: 2001-11-16T21:19:57+09:00 Subject: [ruby-talk:25415] Re: Redefining Fixnum.x(val)? ----- Original Message ----- From: "Masanori Fujita" Newsgroups: comp.lang.ruby To: "ruby-talk ML" ; Sent: Friday, November 16, 2001 1:07 PM Subject: [ruby-talk:25412] Redefining Fixnum.x(val)? > Hello. > > I'm new to Ruby and hav just tried out an example from "Programming Ruby" in > which the +-Operator of the Fixnum-Class should be redefined. > > I executed the following code: > > class Fixnum > alias oldPlus + > def +(other) > oldPlus(other).succ > end > end > > puts 1 + 2 > > The interpreter seems to end with a stack overflow. So what is the problem > with this piece of code? It works just fine, the first time I run it. If it is run a second time, in the same interpreter, then the old and new plus gets identical, and the result is an infinte loop. When running the script from the command line, there should be no problem. What do you use for running the program? This happens for example in RubyWin (Windows), where the same interpreter instance is reused. That means that changes you made in a class, are there also the second time you run a program. I think RubyWin is really convienient, (and handles utf-8 very well), but one should be aware of the interpreter reuse issue. Otherwise it do cause unexpected behaviour. /H ikso.net