What's the opposite of chr() in Ruby?

前端 未结 10 881
自闭症患者
自闭症患者 2020-12-23 12:53

In many languages there\'s a pair of functions, chr() and ord(), which convert between numbers and character values. In some languages, ord()

10条回答
  •  旧时难觅i
    2020-12-23 13:36

    In Ruby up to and including the 1.8 series, the following will both produce 65 (for ASCII):

    puts ?A
    'A'[0]
    

    The behavior has changed in Ruby 1.9, both of the above will produce "A" instead. The correct way to do this in Ruby 1.9 is:

    'A'[0].ord
    

    Unfortunately, the ord method doesn't exist in Ruby 1.8.

提交回复
热议问题