ASCII value of character in Ruby ASCII value of character in Ruby ruby ruby

ASCII value of character in Ruby


The String#ord method will do the trick:

ruby-1.9.2-p136 > 'x'.ord => 120 ruby-1.9.2-p136 > '0'.ord => 48 ruby-1.9.2-p136 > ' '.ord => 32 


You can also use

ruby-2.0.0p353 > "x".sum=> 120ruby-2.0.0p353 > "a string".sum=> 792 

The 'sum' method will find the sum of all character codes, but if you put only one character it will give you the code of that one only.