Jeremy
Jeremy

Reputation: 22415

Integer value of a character in ruby?

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"

Upvotes: 43

Views: 43869

Answers (4)

lokeshjain2008
lokeshjain2008

Reputation: 1891

For those, who are looking for the opposite of ord. We have chr

>> "A".ord
=> 65
>> 65.chr
=> "A"

Upvotes: 37

Chris Johnsen
Chris Johnsen

Reputation: 224689

You probably want String#ord:

% irb
ruby-1.9.2-head > 'a'.ord
 => 97 

Upvotes: 69

A B
A B

Reputation: 8866

Note that if you want to write code that is compatible with both Ruby 1.8 and Ruby 1.9, you may want to use String#each_byte like this:

$ irb
>> 'a'.each_byte.first
=> 97

Upvotes: 3

deeb
deeb

Reputation: 1382

?a will return the ASCII value of the char a

Upvotes: -2

Related Questions