Difference between [0..0] and [0]

I am trying to get at the first character in a string and when I say
@string[0] I get an integer value, but when I say @string[0…0] I get
the character. Why is that? Thanks,

-S

Strings are not treated the same as arrays. A single number in the
brackets gives you the ASCII value of the character at that position.
If you want the first character, I’d use @string[0,1] which gives you
a substring starting at the 0th position, with a length of 1.


Chris

On Nov 16, 2007, at 5:22 PM, Chris E. wrote:

I am trying to get at the first character in a string and when I say
@string[0] I get an integer value, but when I say @string[0…0] I get
the character. Why is that? Thanks,

-S

BUT, this will change in Ruby 2.0 so that “abc”[0] will be “a” rather
than 97

It seems to be true that “something”[n] is the same value as
“something”[n…n][0] for all n

-Rob

Rob B. http://agileconsultingllc.com
[email protected]