From: Austin Ziegler Date: 2002-11-26T13:29:05+09:00 Subject: Re: Things That Newcomers to Ruby Should Know (11/24/02) On Tue, 26 Nov 2002 12:28:15 +0900, Gavin Sinclair wrote: >>> It's applicable to a small subset of the total set of classes. >>> Really, x++ is the same as x.succ!, and x.succ! won't work >>> because there's no assignment and you can't change self. >> I don't follow that. Why is it that I can do string.chomp! but it >> is not conceivable to do x.succ!? Aren't you changing self with >> string.chomp!? > No. Hopefully the following makes sense. More to the point: irb(main):004:0> s = "Hello" "Hello" irb(main):005:0> s.id 20841200 irb(main):006:0> s.succ! "Hellp" irb(main):007:0> s.id 20841200 irb(main):008:0> > The prevailing point is that "x = 4; x++" translates into Ruby > most closely as > x = 4 > x.succ! > > Of course, Integer#succ! is not defined, for good reason. What > does 4.succ! mean? Turn all 4's into 5's? > > 4 is an unmodifiable object. "Hello".chomp! is fine because > "Hello" is a modifiable object. Actually, I think that the proper term is that 4 is an immediate object. It's the immediacy (and the "singleton-object" nature of Fixnums) which makes the difference. This leads, of course, to an interesting question. How can one implement a Fixnum-like behaviour ("singleton-object") in any given Ruby class? (That is, one and only one instance of a given value.) -austin -- Austin Ziegler, austin@halostatue.ca on 2002.11.25 at 23.26.11