Kyle Decot Kyle Decot - 1 year ago 79
Ruby Question

String#[] Differences between Ruby 1.8.7 and 1.9.3

I'm attempting to upgrade an old rails 2.x/ruby 1.8.7 application but I'm
running into an issue. I have the string

and in my code
I have something that looks like:


In 1.8.7 this returns the Fixnum
. In ruby 1.9.3 (and newer) however this
returns the String

My question is two part: First, what's happening here, and secondly how can I
get a similar behavior to what's being returned in 1.8.7 in newer versions of

Update #1

As some have suggested I tried
but this however doesn't work (it does work in
though) as it errors with:

NoMethodError: undefined method `[]' for #<Enumerator: "\x80\u0001\u0002\u0004\b\u0010":bytes>

Answer Source

To get the old behavior, use String#getbyte, as in:


Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download