2.15. Converting Between Characters and ASCII Codes

In Ruby, a character is already an integer. This behavior is slated to change in 2.0 or perhaps sooner. In future versions of Ruby, the current plan is to store a character as a one-character string.

str = "Martin"
print str[0]        # 77

If a Fixnum is appended directly onto a string, it is converted to a character:

str2 = str << 111   # "Martino"

Get The Ruby Way: Solutions and Techniques in Ruby Programming, Second Edition now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.