« On LiberalĀ Arts

Ruby Quirks - String#[]

Ruby String class has [] operator that allows us to get a substring for a given string. For example:

name = "Ashish"
str = name[2,3] # str contains "his"

However, if you pass a single index to the [] operator, the response is not something you would expect. For example:

char = name[1] # char contains 115, the ASCII representation of the character s

While we are in the topic of characters and their ASCII representation, Ruby provides a ? operator to evaluate the ASCII representation of a string. For example:

?a  # returns  97
?A  # returns 65
?s  # returns 115
?AA # results in  SyntaxError

This quirk can be seen in Ruby 1.8.7 and earlier versions of Ruby. However, from 1.9.1, the behavior is as expected. For example, in 1.9.1 and beyond:

?a          # returns a
"Ashish"[1] # returns s
Shovel vs. Plus Equals operator in Ruby »
blog comments powered by Disqus