From: gwtmp01@... Date: 2006-06-02T08:43:56+09:00 Subject: Re: Adding an Increment Operator? On Jun 1, 2006, at 7:15 PM, Dave Howell wrote: > As Logan pointed out in a different thread: > >> % cat increment.rb >> class Fixnum >> def increment >> self = self + 1 >> end >> end >> >> a = 1 >> a.increment >> >> >> % ruby increment.rb >> -:13: Can't change the value of self >> self = self + 1 >> ^ > > So, how WOULD I create an .increment operator? I get why I can't > put it in class Fixnum (well, I think I know why); Fixnums are > symbol-ish-ly unalterable. 5.increment should fail, for sanity's sake. Fixnum objects aren't containers for integer values. Each Fixnum object represents a different integer via a one-to-one mapping. So just as you can't increment the integer 1 you can't increment the Fixnum that represents the integer one. The concept of 'the next integer' works though and that is available via Fixnum#succ Ruby doesn't allow you to use assignment to bind self to a new object either so self = is a syntax error. > So I have to add .increment to class . . . hmm. I can't add it to > variables; they're just references. But I want my_var.increment to > change 'my_var' so that it references the next integer in line. myvar = myvar.succ Gary Wright