Finding Character code Ruby 1.8.7 and 1.9.2


Hi friends,

In Ruby 1.8.7, we can use “?” operator to find the “character code from a character.?a evaluates to the integer 97. Now we can see some examples via “irb”

Ex: (ruby 1.8.7)

> ?a # it shows character code of "a"
=> 97

But in Ruby 1.9.2, we can’t use the “?” operator to find character code from a character.In 1.9.7 , syntax “?a”  is now the same as "a".  There is now an “ord” method of strings to convert a character into its integer code. So we can use the”.ord” method.

Ex:  (ruby 1.9.2)

> ?a    # it won't show the char code, just show as "a"
 => "a"
> "a".ord  # This method ly show char code of "a" in 1.9.7
=> 97

Source:

http://en.wikibooks.org/wiki/Ruby_Programming/ASCII

http://rosettacode.org/wiki/Character_codes

🙂 🙂 🙂

Advertisements

One thought on “Finding Character code Ruby 1.8.7 and 1.9.2

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s