From: Austin Ziegler Date: 2002-07-08T05:13:49+09:00 Subject: Re: is there a better string.each? On Mon, 8 Jul 2002 04:47:33 +0900, Tom Sawyer wrote: >> Precisely; no more than I would say a String "is a"[n] Array. A >> string is a string. > do me a favor and just think about this for awhile. take a look at > c/c++, think about how strings are handled by any programming > language, under the hood. i feel you two are talking a bit off the > cuff and therefore are getting a bit strung up ;-) C and C++ are very bad examples of this because they have notoriously poor string handling. Look at Java and you'll see that while the implementation of String and StringBuffer are typically done with Array, they are not themselves descendents Array objects. They implement the interface CharSequence which gives them the ability to be treated (more or less) as arrays. I think the mistake that you're making is that you're considering the implementation and the interface to be the same -- and I don't think that this is necessarily the case. You *expect* strings to act like arrays because that's the way that C and C++ do it. I know this is how C and C++ do this and curse it every time I have to deal with C and C++ and strings. >> If, in ruby, we want to implement it as an array, fair enough. > by your reasoning: if a string is a string, how can it be > implemented as an array? > > but at least you arn't adverse to the notion. that at least helps. The array is an internal representation of the string, but the interface does not necessarily have to expose that internal representation -- and indeed, probably should NOT do so, in the interest of good OO. To take Java's position on this, a string can be represented as a CharSequence (an Array, if you will), but that representation does not mean that it is necessarily an Array. Even if it *is* implemented that way, it's not a good idea for users to expect it to be so. Who knows? When quantum computers are common, then it might actually be easier to deal with strings implemented as a hypercube or something equally strange. Should users have to know that strings are implemented as a hypercube? No. They should know that they are Strings, and that's it. -austin -- Austin Ziegler, austin@halostatue.ca on 2002.07.07 at 16.02.54