Reputation: 3538
I'm trying use the Str[fixnum] to return a specific portion of a string.
# Given
Str = "James"
Str[0]
# Should yield `"J"'.
# Instead its yielding `74`.
The strange thing is yesterday evening it yielded the "J"
Does anyone have any idea of what might be going on here. Have I inadvertently changed a setting that is causing the function to return the ascii character number. I'm stumped.
Update: I have confirmed that 74 is in fact the ascii number representation for "J". So the question is the function return the ascii instead of the actual character.
Upvotes: 1
Views: 58
Reputation: 370112
String#[]
with a single integer i
as an argument returns the i
th byte as an integer in ruby 1.8. In ruby 1.9 it returns a one-character string.
So when you tried it yesterday, you were probably using ruby 1.9, while you're using 1.8 today.
To get a one-character string in ruby 1.8, you can use str[i, 1]
, which has the same behavior in 1.8 and 1.9.
Upvotes: 4