57

I'm in a situation where I need the ASCII value of a character (for Project Euler question #22, if you want to get specific) and I'm running into an issue.

Being new to ruby, I googled it, and found that ? was the way to go: ?A or whatever. But when I incorporate it into my code, the result of that statement is the string "A"—no character code. Same issue with [0] and slice(0), both of which should theoretically return the ASCII code.

The only thing I can think of is that this is a ruby version issue. I'm using 1.9.1-p0, having upgraded from 1.8.6 this afternoon. I cheated a little going from a working version of Ruby, in the same directory, I figured I probably already had the files that don't come bundled with the .zip file, so I didn't download them.

So why exactly are all my ASCII codes being turned into actual characters?

Andrew Marshall
  • 95,083
  • 20
  • 220
  • 214
dorr
  • 816
  • 1
  • 6
  • 11

6 Answers6

77

Ruby before 1.9 treated characters somewhat inconsistently. ?a and "a"[0] would return an integer representing the character's ASCII value (which was usually not the behavior people were looking for), but in practical use characters would normally be represented by a one-character string. In Ruby 1.9, characters are never mysteriously turned into integers. If you want to get a character's ASCII value, you can use the ord method, like ?a.ord (which returns 97).

Chuck
  • 234,037
  • 30
  • 302
  • 389
  • Just to complete, version 1.9.3 or later doesn't need '?' sign before the letter, we can get the Integer value just with: a.ord – yat0 Dec 25 '14 at 19:45
  • 5
    @polska: I think you might have misunderstood. The ? signifies that we want the character 'a', so writing `?a.ord` gives 97, whereas simply writing `a.ord` looks for a variable or method called `a` and will throw a NameError if there isn't anything with that name or a NoMethodError if the variable `a` does not contain a string. – Chuck Dec 26 '14 at 22:23
  • 2
    yeah, you're right.. I've misunderstood. It works with ?a.ord or "a".ord – yat0 Dec 28 '14 at 17:52
39

How about

"a"[0].ord

for 1.8/1.9 portability.

Ryan Calhoun
  • 2,343
  • 22
  • 26
16

Ruby Programming/ASCII

In previous ruby version before 1.9, you can use question-mark syntax.

?a

After 1.9, we use ord instead.

'a'.ord    
hiveer
  • 660
  • 7
  • 17
12

For 1.8 and 1.9

?a.class == String ? ?a.ord : ?a

or

"a".class == String ? "a".ord : "a"[0]
sth
  • 222,467
  • 53
  • 283
  • 367
mark
  • 121
  • 1
  • 2
  • 1
    This last one errors for me on 1.8.7: `>> "a".class == String ? "a".ord : "a"[0]` produces error `NoMethodError: undefined method \`ord' for "a":String from (irb):3` – Dolan Antenucci Dec 06 '13 at 18:35
8

Found the solution. "string".ord returns the ascii code of s. Looks like the methods I had found are broken in the 1.9 series of ruby.

dorr
  • 816
  • 1
  • 6
  • 11
  • 1
    It's better to edit your original post instead of posting a separated reply to your own question. – pierrotlefou Aug 13 '09 at 06:41
  • 7
    @pierr - But this is an answer to the question, not just an update of the question – rampion Aug 13 '09 at 08:44
  • 5
    The methods aren't broken. Ruby 1.9 defines their behavior differently, as Chuck explained. – Telemachus Aug 13 '09 at 09:39
  • Yeah, broken was the wrong word. They no longer work as I expected based on previous versions. The new usage was what I originally expected them to do, and makes a lot more sense. – dorr Aug 13 '09 at 20:23
-3

If you read question 22 from project Euler again you'll find you you are not looking for the ASCII values of the characters. What the question is looking for, for the character "A" for example is 1, its position in the alphabet where as "A" has an ASCII value of 65.

Schechter
  • 224
  • 2
  • 5
  • 1
    Surely you could get the position in the alphabet by substracting 64 from the ASCII value of the uppercase letter. This answer should be a comment. – Nils Luxton Jun 02 '14 at 13:29
  • @NilsLuxton, Maybe this should have been a comment, but it's a better answer to the actual question they're looking for. If you read the Project Euler question being referred to, the ACSII value is not what they're looking for. Did not deserve a down vote. – Schechter Jul 01 '14 at 02:51
  • 2
    The question was "How do I get the ASCII value of a character" not "Can someone clarify #22 in Project Euler". The mention of the problem at hand is just context. Their solution may well (and likely does) involve finding the ASCII value first and calculating from that point on. I'm sure the OP appreciates your clarification, but as it stands, it is not an answer to the question they asked. I'm probably being a little strict on the rules here (and an S.O. noob), but if this answer was a comment, it would get an upvote from me. – Nils Luxton Jul 01 '14 at 03:58