Accessing character code in Ruby 1.9.1

Hi all,

I read from to the docs[1] that string[index] should do the
following: “If passed a single Fixnum, returns the code of the
character at that position.”

This is working as documented with Ruby 1.8.6, but it isn’t with Ruby
1.9.1p129 (on my WinXP machine). Why is this so?

Thanks for any hints
Thomas W.

[1] http://ruby-doc.org/core/classes/String.html#M000771

On Thursday 24 September 2009, ThomasW wrote:

|Thomas W.
|
|[1] http://ruby-doc.org/core/classes/String.html#M000771
|

Because you’re looking at the documentation for ruby 1.8, not at that
for ruby
1.9. The documentation for ruby 1.9, which you can find at
http://ruby-doc.org/ruby-1.9/index.html, states that in ruby 1.9,
String#[]
returns the character at the given position, not its code. To obtain the
code
of the character, you can use its ord method:

‘a’.ord
=> 97

I hope this helps

Stefano

On 24 Sep., 15:47, Stefano C. [email protected] wrote:

|

‘a’.ord
=> 97

I hope this helps

Stefano

Yes, this helps very much! Thanks for pointing me to this.

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs