Bug in String element reference []?

Hi all,
i found out today that if one will assign string to a variable like
this:

a = “08800123”

and then want to have a substring like this:

a[2]

the result is:
=> 56

it gets even better:
#a[2,1]
=> “8”

i would expect same result from both.

Also a[2] is not what specification says about String class.
Should i file a bug?

Best greetings,
Paweł Wielgus.

2011/9/26 Paweł Wielgus [email protected]

=> “8”

i would expect same result from both.

Also a[2] is not what specification says about String class.
Should i file a bug?

No, it is not a bug. It is the behavior in Ruby 1.8, which was changed
in
1.9

Try the --1.9 flag

[email protected] [jruby-1.6.4] ~$ jruby -S irb
jruby-1.6.4 :001 > “08800123”[2]
=> 56
jruby-1.6.4 :002 > “08800123”[2, 1]
=> “8”

[email protected] [jruby-1.6.4] ~$ jruby --1.9 -S irb
jruby-1.6.4 :001 > “08800123”[2]
=> “8”
jruby-1.6.4 :002 > “08800123”[2, 1]
=> “8”
jruby-1.6.4 :003 >

This is not a bug but the difference between Ruby 1.8.7 semantics and
Ruby 1.9.2 semantics. I suspect you are looking at Ruby 1.9
documentation.

-Tom

1.9.2 ~/work/Purugin master 650% ruby -e ‘a = “08800123”; p a[2],
a[2,1]’
“8”
“8”
1.9.2 ~/work/Purugin master 651% rvm use 1.8.7
Using /Users/enebo/Developer/.rvm/gems/ruby-1.8.7-p334
1.8.7 ~/work/Purugin master 652% ruby -e ‘a = “08800123”; p a[2],
a[2,1]’
56
“8”
1.8.7 ~/work/Purugin master 653% jruby -e ‘a = “08800123”; p a[2],
a[2,1]’
56
“8”
1.8.7 ~/work/Purugin master 654% jruby --1.9 -e ‘a = “08800123”; p a[2],
a[2,1]’
“8”
“8”

2011/9/26 Pawe W. [email protected]:

=> “8”
To unsubscribe from this list, please visit:

http://xircles.codehaus.org/manage_email


blog: http://blog.enebo.com twitter: tom_enebo
mail: [email protected]

Hi Ayose and Thomas,
of course i was looking at 1.9 specs
that’s how googling “ruby string” ends up,
i wasn’t aware of this up until now.

Shame on me and sorry for your wasted time.

Best greetings,
Pawe W…

2011/9/26 Thomas E Enebo [email protected]:

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