String expression in Ruby 1.8 different than higher versions

breaking_changes

#1

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.


#2

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


#3

thanks: :point_up_2:


#4

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.)


Ruby API in 2017
#5

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


#6

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).


#7

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


#8

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