Ruby 1.8 和 UTF-8 字符串大小写语句比较
我有一个 Rake 任务(在 lib/tasks 目录中),我在共享虚拟主机上使用 cron 运行该任务。 问题是我想使用 case 语句比较 UTF-8 字符串,但我的源代码不是 UTF-8 编码的。 如果我将源代码保存为 UTF-8,当我尝试启动它时会出现错误:(
我必须做什么?
可以从外部 UTF-8 txt 文件读取此字符串吗?
PS 我正在使用 Ruby 1.8
P.S. 我的意思是比较这边走:
result = case utf8string
when 'АБВ': 1
when 'ГДИ': 2
when 'ЙКЛ': 3
when 'МНО': 4
else 5
end
I have a Rake task (in lib/tasks directory) that I run with cron on my shared web hosting. The problem is that I want to compare a UTF-8 string using case statment but my source code is not UTF-8 encoded. If I save source code as UTF-8 there is error when I try to start it :(
What I have to do?
May be read this strings from external UTF-8 txt file?
P.S. I'm using Ruby 1.8
P.S. I mean compare this way:
result = case utf8string
when 'АБВ': 1
when 'ГДИ': 2
when 'ЙКЛ': 3
when 'МНО': 4
else 5
end
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
我发现我的问题不在于 case 语句,
问题在于当我以 UTF-8 格式保存源代码时,我的文本编辑器在开头添加 3 个字节(BOM)以指示编码是 UTF-8。
UTF-8、UTF-16、UTF-32 和 BOM
我得到的错误是:
I found that my problem was not in case statment
The problem was that when I save my source code in UTF-8 format, my text editor add 3 bytes (BOM) at the beginning to indicate that encoding is UTF-8.
UTF-8, UTF-16, UTF-32 & BOM
The error that I get was:
我想说您需要更改文本编辑器,因为 UTF-8 不需要 BOM。 UTF-8 不依赖于字节顺序。 有关详细信息,请参阅链接文本。
I'd say you need to change your text editor as BOM is not needed for UTF-8. UTF-8 is not byte-order dependent. See link text for details.
尝试使用 Rails 的 ActiveSupportmb_chars 方法a> 框架:
Try using the
mb_chars
method from Rails' ActiveSupport framework: