Any ideas as to why a[0] is returning a FixNumb object?
For some reason Matz thought it was more useful to define the [] method
for the String class to return the ascii value of the character. As you
discovered, if you specify a length as the second argument, you get the
actual characters. Applying that knowledge, if you specify a length of
1, you will get a single character. So the expression:
str[0,1]
will return a character in both ruby 1.8 and ruby 1.9.
If I were you, I would upgrade to ruby 1.9.2(or whatever the
latest version is), and learn the latest version.
That is the way String indexing works in Ruby, if you only pass one
parameter you’ll get the ASCII value of the character. If you would like
to get “h” you must do a[0,1] where the first parameter tells the
parser to get the first character of the String and the second parameter
tells the number of characters you want to get from that position.
Hope this helps
Javier Hidalgo
This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.