Question regarding design of the String Class

On Apr 24, 2007, at 4:35 AM, Michael W. Ryder wrote:

If I were getting back a character or string I would not have a
problem. The problem is that every other use of [] in the string
class returns a string or nil, this one returns an integer. I was
curious if this was necessary for some other part of the language
or just an “accident”.

A string serves a dual purpose in Ruby, it is a container for textual
data and it is also a container for binary data (an array of bytes).

When dealing with binary data, the ability to extract a single byte
at an offset is a common operation and so

buffer[offset] # returns fixnum

is a very handy syntax. As you’ve noted, that isn’t as useful when
you consider a string to be text and I believe the direction in Ruby
1.9 is to make the syntax lean towards the string as text
interpretation.
In Ruby 1.9, s[offset] return the one character string starting at
offset, where offset is interpreted within the context of the string
encoding. That is to say, offset is not a byte offset in this case
since the goal is to support a variety of encodings.

Gary W.