Ruby 1.8 and UTF-8 string case statement compare

2019-07-22 18:41发布

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

3条回答
SAY GOODBYE
2楼-- · 2019-07-22 19:11

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楼-- · 2019-07-22 19:24

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.

查看更多
神经病院院长
4楼-- · 2019-07-22 19:27

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
查看更多
登录 后发表回答