gems – Working with Cyrillic Strings in Ruby

Question:

Hello everyone!

Many sources write about Cyrillic strings, and using the upcase / downcase / capitalize methods with them is possible only when connected, for example, the activesupport gem (chars method).

Given code:

require "rubygems"
require "active_support"
s = "привет"
puts s
puts s.upcase
puts s.chars.upcase

In theory, it should return:

привет
привет
ПРИВЕТ

But in fact it returns:

привет
привет
upper.rb:10:in `<main>': undefined method `upcase' for ["п", "р", "и", "в", "е", "т"]:Array (NoMethodError)

Please tell me what needs to be done so that the above methods work correctly with the Cyrillic alphabet. Without using the activesupport gem, the methods work correctly only with English. It is a mistake using this gem.

Versions: ruby ​​2.1.2p95 (2014-05-08 revision 45877) [x86_64-linux] / Rails 4.1.6

Thanks a lot in advance for your help!

Answer:

Use the .mb_chars method

http://api.rubyonrails.org/classes/ActiveSupport/Multibyte/Chars.html

http://apidock.com/rails/String/mb_chars

Scroll to Top