Why does a string index return an integer value instead of a character?

bash, executable, ruby

Solution

Prior to 1.9, Ruby returned the ASCII character code for single characters. 1.9+ switched to returning the character itself:

irb(main):001:0> RUBY_VERSION
=> "1.8.7"
irb(main):002:0> 'foo'[0]
=> 102
irb(main):001:0> RUBY_VERSION
=> "1.9.3"
irb(main):002:0> 'foo'[0]
=> "f"

Using the constant `RUBY_VERSION` is a good way to debug this sort of problem quickly.

Problem

It appears that when putting Ruby code in an executable script, the index of a String behaves differently than it does in IRB or by running the Ruby code directly. For example: ``` $ cat > test #!/usr/bin/ruby -w puts 'hello'[0] $ chmod +x test $ ./test 104 $ ruby -e "puts 'hello'[0]" h ``` Why is this? And, how do I make the executable script behave the same as "normal" Ruby code?

Original source