使用' ?”(问号)失败

时间:2020-12-31 23:31: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.

我现在需要一个字符的ASCII值(对于Project Euler问题#22,如果您想了解得更具体的话),我遇到了一个问题。

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.

作为ruby的新手,我用谷歌搜索了一下,发现了吗?应该怎么做?A或者别的什么。但是,当我将它合并到代码中时,这个语句的结果是字符串“A”-no字符代码。[0]和slice(0)也存在同样的问题,它们理论上都应该返回ASCII代码。

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.

我唯一能想到的就是这是一个ruby版本的问题。我用的是1.9.1-p0,从今天下午的1.8.6升级。在同一个目录下,我从Ruby的工作版本中骗过了一点,我想我可能已经有了没有与.zip文件捆绑在一起的文件,所以我没有下载它们。

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

那么,为什么我所有的ASCII码都变成了实际的字符呢?

6 个解决方案

#1


63  

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).

Ruby在1.9之前对字符的处理有些不一致。?a和“a”[0]将返回一个整数,该整数表示字符的ASCII值(这通常不是人们所寻找的行为),但在实际使用中,字符通常由一个字符字符串表示。在Ruby 1.9中,字符永远不会神秘地变成整数。如果您想获得一个字符的ASCII值,您可以使用ord方法,比如?奥德(返回97)。

#2


33  

How about

如何

"a"[0].ord

for 1.8/1.9 portability.

1.8/1.9可移植性。

#3


10  

For 1.8 and 1.9

为1.8和1.9

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

or

"a".class == String ? "a".ord : "a"[0]

#4


7  

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.

找到了解决方案。“弦”。ord返回s的ascii码。看起来我发现的方法在1.9系列ruby中被破坏了。

#5


5  

Ruby Programming/ASCII

Ruby编程/ ASCII

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

在1.9之前的ruby版本中,您可以使用问号语法。

?a

After 1.9, we use ord instead.

在1.9之后,我们使用了ord。

'a'.ord    

#6


-2  

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.

如果你再次阅读project Euler的问题22,你会发现你不是在寻找字符的ASCII值。问题是要找的,例如A字符是1,它在字母表中的位置是A,它的ASCII值是65。

#1


63  

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).

Ruby在1.9之前对字符的处理有些不一致。?a和“a”[0]将返回一个整数,该整数表示字符的ASCII值(这通常不是人们所寻找的行为),但在实际使用中,字符通常由一个字符字符串表示。在Ruby 1.9中,字符永远不会神秘地变成整数。如果您想获得一个字符的ASCII值,您可以使用ord方法,比如?奥德(返回97)。

#2


33  

How about

如何

"a"[0].ord

for 1.8/1.9 portability.

1.8/1.9可移植性。

#3


10  

For 1.8 and 1.9

为1.8和1.9

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

or

"a".class == String ? "a".ord : "a"[0]

#4


7  

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.

找到了解决方案。“弦”。ord返回s的ascii码。看起来我发现的方法在1.9系列ruby中被破坏了。

#5


5  

Ruby Programming/ASCII

Ruby编程/ ASCII

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

在1.9之前的ruby版本中,您可以使用问号语法。

?a

After 1.9, we use ord instead.

在1.9之后,我们使用了ord。

'a'.ord    

#6


-2  

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.

如果你再次阅读project Euler的问题22,你会发现你不是在寻找字符的ASCII值。问题是要找的,例如A字符是1,它在字母表中的位置是A,它的ASCII值是65。