Geo
Geo

Reputation: 96767

How to get code of unicode character using Ruby 1.8.6?

If I add this to the beginning of my script:

$KCODE = 'UTF8'
require 'jcode'

then I can walk over every char of a word containing unicode characters. Imagine a word containing umlauts or something, and I iterate over them like this:

word.each_char do |c|
  # do something with c
end

If c is a unicode character and I print it's size, it will be 2 ( composed of 2 characters ). How can I get c's code? Is there some formula I could use, or is there something in the std lib that can do this?

Upvotes: 1

Views: 802

Answers (1)

Mladen Jablanović
Mladen Jablanović

Reputation: 44080

Should be:

c.unpack('U*').first

Upvotes: 3

Related Questions