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

前端 未结 10 883
自闭症患者
自闭症患者 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条回答
  •  甜味超标
    2020-12-23 13:49

    I'm writing code for 1.8.6 and 1.9.3 and I couldn't get any of these solutions to work in both environments :(

    However, I came across another solution: http://smajnr.net/2009/12/ruby-1-8-nomethoderror-undefined-method-ord-for-string.html

    That didn't work for me either but I adapted it for my use:

    unless "".respond_to?(:ord)
      class Fixnum
        def ord
          return self
        end
      end
    end

    Having done that, then the following will work in both environments

    'A'[0].ord

提交回复
热议问题