I am looking to turn the character 'a' into 97 in ruby 1.9.2
Ruby 1.8.7
irb(main):001:0> ?a => 97
Ruby 1.9.2
irb(main):001:0> ?a => "a"
To convert an string to a integer, we can use the built-in to_i method in Ruby. The to_i method takes the string as a argument and converts it to number, if a given string is not valid number then it returns 0.
In Ruby, Integer class is the basis for the two concrete classes that hold whole numbers. These concrete classes are Bignum and Fixnum. Fixnum holds integer values that are shown in the native machine word, whereas Bignum holds the integer value outside the range of Fixnum.
In double quoted strings, you can write escape sequences and Ruby will output their translated meaning. A \n becomes a newline. In single quoted strings however, escape sequences are escaped and return their literal definition. A \n remains a \n .
You probably want String#ord
:
% irb ruby-1.9.2-head > 'a'.ord => 97
For those, who are looking for the opposite of ord
. We have chr
>> "A".ord => 65 >> 65.chr => "A"
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With