Best practice to handle non-english characters in Ruby?
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 a开发者_如何学Pythonnnoying! Is there a best practice to work around this problem?
You can use ruby1.9
$ ruby1.8 -e 'puts "åäö".length'
6
$ ruby1.9 -e 'puts "åäö".length'
3
Just add this command on the top of your file:
# -*- encoding: utf-8 -*-
Hope this helps.
精彩评论