1. Home
  2. Computing & Technology
  3. Ruby

What's New in Ruby 1.9.1? - -The String Index Operator in Ruby 1.9.1
What Does the Question Mark Operator Return?

By Amanda & Michael Morin, About.com

In past Ruby versions, the question mark operator returned the ASCII value of a character. For example, ?g will return the integer 103, the ASCII code for the character lower-case g. This, however, turned out to be less than useful.

In Ruby 1.9, this has changed. The question mark operator now returns a single-character string of that character. For example, ?g will return "g".

To get the old behavior, to get the ASCII value of a string, use the ord method. The ord method returns the ASCII value of the first character of a string.

#!/usr/bin/env ruby

# In Ruby 1.9, this returns a single
# character string
puts ?g

# To get the ASCII value, use the ord method
"g".ord
More Ruby Quick Tips
Explore Ruby
About.com Special Features

Stay connected and entertained with reviews on tips on the latest HDTVs, cellphones and more. More >

Easy ways to connect two computers for networking purposes. More >

  1. Home
  2. Computing & Technology
  3. Ruby
  4. Beginning Ruby
  5. New in Ruby 1.9.1
  6. The Question Mark Operator in Ruby 1.9.1

©2009 About.com, a part of The New York Times Company.

All rights reserved.