String expression in Ruby 1.8 different than higher versions

A question mark followed by a character returns an integer matching the position
of that character in the ASCII table, a standard for representing characters as values.
for example, puts ?x => 120 .

this String Expressions will only work with version ruby-1.8.7.or lower.
with version 1.9 or higher… that string expressing will give another answer on the example; puts ?x… namely => “x”.

i have installed Ruby 1.8 and Ruby 2.3.
is there someone who can tell me, how this is possible…?
maybe there is another string expression for the example i give…?
please let me know.

The new way since Ruby 1.9 is"x".ord

thanks: :point_up_2:

It is possible because the Ruby Core team makes changes between code trunks and even major versions within trunks, that can be “breaking changes” (ie, changes that break Ruby scripts.)

There are mentions of these “breaking changes” in the NEWS files, released along with the Ruby source code.


The String class ordinal position changes also effected the String#[] method.

In the Ruby 1.8 trunk, if only one index value was passed, the method returned the ordinal integer in the ASCII set.

In later Ruby (v1.9.1 and higher) a single index value returns the actual character (a substring of length 1) from the string at that index position. The Core team did this because this is how it works in every other programming language invented, and the old way was unexpected by programmers. (Everyone hated the old way.)

the ?v for example i choose that for better than the one in higer versions of Ruby like “v”.ord
but i know always there is a reason changing thinks.
anyway thanks for the breaking doc news. i bought aRuby book (Apress - Beginning Ruby - From Novice to Professiona (2007)) and they use irb version 1.8.7… so you can imagine… there explanation didn’t match with my version 2.2.3
anyway i shall make notes in that book.

thanks for your explanation

It’s worth getting the latest version of the “pickaxe book”, which is the most comprehensive and comprehensible Ruby reference I have found (I have no connection to the publisher or author. Also you might be able to find a copy cheaper than from the publisher).

yes accidentally i purchased that version today as ebook … looks good.:point_up_2:

This topic was automatically closed after 91 days. New replies are no longer allowed.