Getting an ASCII character code in Ruby using `?` (question mark) fails

后端 未结 6 1002
旧时难觅i
旧时难觅i 2020-12-08 06:01

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 n

6条回答
  •  醉话见心
    2020-12-08 07:03

    For 1.8 and 1.9

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

    or

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

提交回复
热议问题