Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get character's Unicode in Ruby 1.8.7?

To get character's Unicode in Ruby 1.9.2, I use ord:

"я".ord       # => 1103     (It's a Russian letter)

How could I get the Unicode in Ruby 1.8.7 ?

like image 708
Misha Moroshko Avatar asked May 13 '11 00:05

Misha Moroshko


2 Answers

You could use my backports gem. Running with Ruby 1.8.7 (and option -KU to setup $KCODE for utf-8):

require "rubygems"
require "backports/1.8.7/string/ord"
"я".ord # => 1103
like image 154
Marc-André Lafortune Avatar answered Sep 28 '22 07:09

Marc-André Lafortune


Well, I found this nice solution:

"я".unpack('U')[0]     # => 1103
like image 39
Misha Moroshko Avatar answered Sep 28 '22 06:09

Misha Moroshko