Ruby 1.8 and UTF-8 string case statement compare

こ雲淡風輕ζ 提交于 2019-12-24 11:03:43

问题


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

回答1:


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.




回答2:


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.

Q: What is a BOM?

A: A byte order mark (BOM) consists of the character code U+FEFF at the beginning of a data stream, where it can be used as a signature defining the byte order and encoding form, primarily of unmarked plaintext files. Under some higher level protocols, use of a BOM may be mandatory (or prohibited) in the Unicode data stream defined in that protocol.

UTF-8, UTF-16, UTF-32 & BOM

The error that I get was:

1: Invalid char `\357' in expression
1: Invalid char `\273' in expression
1: Invalid char `\277' in expression



回答3:


Try using the mb_chars method from Rails' ActiveSupport framework:

result = case utf8string.mb_chars
   when 'АБВ': 1
   when 'ГДИ': 2
   when 'ЙКЛ': 3
   when 'МНО': 4
   else 5
end


来源:https://stackoverflow.com/questions/353326/ruby-1-8-and-utf-8-string-case-statement-compare

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!