开发者

Best practice to handle non-english characters in Ruby?

开发者 https://www.devze.com 2022-12-24 03:57 出处:网络
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 (

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.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号