# Accessing character code in Ruby 1.9.1

**URL:** <https://www.ruby-forum.com/t/accessing-character-code-in-ruby-1-9-1/176081>\
**Category:** Ruby\
**Created:** [September 24, 2009, 3:25pm UTC](https://www.ruby-forum.com/t/accessing-character-code-in-ruby-1-9-1/176081 "2009-09-24T15:25:26Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![ThomasW](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/f475e1/32.png) [@ThomasW](https://www.ruby-forum.com/u/ThomasW)\
**Post date:** [September 24, 2009, 3:25pm UTC](https://www.ruby-forum.com/t/accessing-character-code-in-ruby-1-9-1/176081/1 "2009-09-24T15:25:26Z")

</div>

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] [class String - RDoc Documentation](http://ruby-doc.org/core/classes/String.html#M000771)

---

<div class="post-metadata">

**Author:** ![ThomasW](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/f475e1/32.png) [@ThomasW](https://www.ruby-forum.com/u/ThomasW)\
**Post date:** [September 24, 2009, 3:47pm UTC](https://www.ruby-forum.com/t/accessing-character-code-in-ruby-1-9-1/176081/2 "2009-09-24T15:47:41Z")

</div>

On Thursday 24 September 2009, ThomasW wrote:

> |Thomas W.  
> |  
> |[1] [class String - RDoc Documentation](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  
[RDoc Documentation](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

---

<div class="post-metadata">

**Author:** ![ThomasW](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/f475e1/32.png) [@ThomasW](https://www.ruby-forum.com/u/ThomasW)\
**Post date:** [September 24, 2009, 9:05pm UTC](https://www.ruby-forum.com/t/accessing-character-code-in-ruby-1-9-1/176081/3 "2009-09-24T21:05:36Z")

</div>

On 24 Sep., 15:47, Stefano C. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> > |
> 
> ‘a’.ord  
> =\> 97
> 
> I hope this helps
> 
> Stefano

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