DZone Snippets is a public source code repository. Easily build up your personal collection of code snippets, categorize them with tags / keywords, and share them with the world

Snippets has posted 5883 posts at DZone. View Full User Profile

Convert Between Characters And Values

05.18.2007
| 39599 views |
  • submit to reddit
        // character to ASCII value: use ?
?a     # => 97
?\n    # => 10

// string to integer: use []
'a'[0]        # => 97
'hallo'[1]    # => 97

// integer / number to character: use .chr
97.chr     # => "a"
10.chr     # => "\n"

//more info: "Ruby Cookbook", O'Reilly    

Comments

Snippets Manager replied on Sat, 2009/05/09 - 1:35am

oops meant "it is now"

Snippets Manager replied on Sat, 2009/05/09 - 1:35am

with ruby 1.9 you cannot use brackets, it is not 'a'.ord # => 97