Nikolai Weibull wrote:
> On 2/5/07, David Flanagan <david / davidflanagan.com> wrote:
> 
>> Does anyone (Matz?) know why the (new in 1.9) String.ord method is
>> restricted to one-character Strings?
> 
> Because it only makes sense on a string of (character) length one.

I have to disagree.  It seems to me that String really needs a method 
that provides the same behavior as the [] does in 1.8.  ord could be 
that method, or I could argue for ordAt().

I find it very unnatural to define a String method that is only valid on 
a restricted subset of Strings.

>> Why can't I pass the index to ord itself?  "ABC".ord(0) => 65
> 
> Because that's complicating matters unnecessarily, and indexing
> shouldn't be part of the interface to a method that turns a string
> into its position in the encoding table of the string in question.

I see no reason why it shouldn't be.  As it stands now, Ruby 1.8 code 
that uses the [] operator must be replaced with [].ord.  One method call 
becomes two.

> You wouldn't expect String#succ to take an index, or String#hex for
> that matter, right?

I think these are bad analogies. If either of these methods required a 
single-character string, then I'd make the same argument for them as I'm 
making for ord.

	David

>  nikolai
> 
>