views:

125

answers:

2

My program file is encoded in UTF-8 so "abc".length == 3 but "åäö".length == 6. I realize that å, ä, ö, etc. are stored as two bytes in UTF-8, and that a Ruby String is a sequence of bytes (not characters), but it is annoying! Is there a best practice to work around this problem?

A: 

Hi Ragnarius,

Just add this command on the top of your file:

# -*- encoding: utf-8 -*-

Hope this helps.

Cyril
Thanks but I still get "åäö".length==6
ragnarius
I think the best way is to update.
Cyril
That won't work for 1.8 afaik.
khelll
+4  A: 

You can use ruby1.9

$ ruby1.8 -e 'puts "åäö".length'
6
$ ruby1.9 -e 'puts "åäö".length'
3
gnibbler
Thanks, it worked great!
ragnarius