开发者

How to get the i'th character from utf-8 String in Ruby 1.8.7?

开发者 https://www.devze.com 2023-04-01 05:11 出处:网络
Given the following const开发者_StackOverflowant: RUSSIAN_LOWERCASE_ALPHABET = \"абвгдеёжзийклмнопрстуфхцчшщъыьэюя\"

Given the following const开发者_StackOverflowant:

RUSSIAN_LOWERCASE_ALPHABET = "абвгдеёжзийклмнопрстуфхцчшщъыьэюя"

When trying to get one letter:

content_tag(:span, RUSSIAN_LOWERCASE_ALPHABET[0])

Ruby 1.9.2 does the work as expected (I see the letter in a browser), while with Ruby 1.8.7 (my production environment) I see a number instead (e.g. 320).

I tried to change this to:

content_tag(:span, RUSSIAN_LOWERCASE_ALPHABET[0..0])

but it didn't help (I see a question mark in a diamond instead).

How could I solve this problem ?


In Rails you have Multibyte support, just convert your string to wrapper class from Rails:

RUSSIAN_LOWERCASE_ALPHABET = "...".mb_chars

and then you can select character with RUSSIAN_LOWERCASE_ALPHABET[0].

0

精彩评论

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

关注公众号