accessing character code in Ruby 1.9.1

T

ThomasW

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
 
S

Stefano Crocco

|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
|

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
 
T

ThomasW

|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
|

Because you're looking at the documentation for ruby 1.8, not at that forruby
1.9. The documentation for ruby 1.9, which you can find athttp://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

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

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,997
Messages
2,570,241
Members
46,831
Latest member
RusselWill

Latest Threads

Top