Accessing strings

If I something like word = “ruby”
can’t I do something like (I thought it was possible)

word[0] -> r
word[1] -> u
word[2] -> b
word[3] -> y

Trying something like that now and it’s giving me “weird” numbers

Yet I’m trying it (both program and irb) and it gives me weird numbers

irb(main):010:0> word = “ruby”
=> “ruby”
irb(main):011:0> puts word[0]
114
=> nil
irb(main):012:0> puts word[2]
98
=> nil
irb(main):013:0> puts word[3]
121
=> nil

So what am I doing wrong ?
TIA
Stuart

Dark A. wrote:

Yet I’m trying it (both program and irb) and it gives me weird numbers
121
=> nil

So what am I doing wrong ?
TIA
Stuart

We just discussed this:
http://blade.nagaokaut.ac.jp/cgi-bin/scat.rb/ruby/ruby-talk/200453

Also check the documentation.
http://ruby-doc.org/core/classes/String.html#M001843

-Justin

-Justin

On Jul 6, 2006, at 5:01 PM, Justin C. wrote:

Yet I’m trying it (both program and irb) and it gives me weird
numbers

irb(main):010:0> word = “ruby”
=> “ruby”
irb(main):011:0> puts word[0]
114

Also check the documentation.
http://ruby-doc.org/core/classes/String.html#M001843

Or even on your own machine:

$ ri String#[]
… ri output


Eric H. - [email protected] - http://blog.segment7.net
This implementation is HODEL-HASH-9600 compliant

http://trackmap.robotcoop.com

On Jul 6, 2006, at 5:17 PM, Dark A. wrote:

Ahh…so those numbers are the character codes ?
$ ri String#[]
[…]
Element Reference—If passed a single +Fixnum+, returns the code
of the character at that position. If passed two +Fixnum+ objects,
[…]

See also Integer#chr.


Eric H. - [email protected] - http://blog.segment7.net
This implementation is HODEL-HASH-9600 compliant

http://trackmap.robotcoop.com

Ahh…so those numbers are the character codes ?

Stuart

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