1. Technology
You can opt-out at any time. Please refer to our privacy policy for contact information.

What's New in Ruby 1.9.1? --The String Index Operator in Ruby 1.9.1

What Does the Question Mark Operator Return?


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
See More About
  1. About.com
  2. Technology
  3. Ruby
  4. Beginning Ruby
  5. New in Ruby 1.9.x
  6. The Question Mark Operator in Ruby 1.9.1

©2014 About.com. All rights reserved.