Apply .capitalize to Cyrillic in ruby

I want to use string strings in an array with ruby

This is my code:

headermonths = ["","","","","","","","","","",""]

headermonths.each {|month| month.capitalize!}
puts headermonths

I get the following output:












if you print an array with:

 print headermonths    

I get the following

["\u044F\u043D\u0432\u0430\u0440\u044F", "\u0444\u0435\u0432\u0440\u0430\u043B\u044F", "\u043C\u0430\u0440\u0442\u0430", "\u0430\u043F\u0440\u0435\u043B\u044F", "\u043C\u0430\u044F", "\u0438\u044E\u043D\u044F", "\u0438\u044E\u043B\u044F", "\u0430\u0432\u0433\u0443\u0441\u0442\u0430", "\u043E\u043A\u0442\u044F\u0431\u0440\u044F", "\u043D\u043E\u044F\u0431\u0440\u044F", "\u0434\u0435\u043A\u0430\u0431\u0440\u044F"]

But I would like to have an output like:

How can I achieve this using the ruby ​​method?

+4
source share
3 answers

You can use unicode gem

require 'unicode'

headermonths = ["","","","","","","","","","",""]

headermonths.map! {|month| Unicode::capitalize month }
puts headermonths
# >> ["", "", "", "", "", "", "", "", "", "", ""]
+2
source

Standalone solution:

# From : https://en.wikipedia.org/wiki/Cyrillic_alphabets :
upcase   = ""
downcase = ""

headermonths = ["","","","","","","","","","",""]

headermonths.each{|word| word[0] = word[0].tr(downcase,upcase)}
# => ["", "", "", "", "", "", "", "", "", "", ""]

If you want to use it with words in Latin and Cyrillic alphabets:

headermonths.each{|word| word[0] = word[0].tr(downcase,upcase).upcase }

With ActiveSupport

You can use ActiveSupport :: Multibyte :

require 'active_support/core_ext/string/multibyte'
"".mb_chars.capitalize.to_s #=> ""

So your script becomes:

require 'active_support/core_ext/string/multibyte'

headermonths = ["","","","","","","","","","",""]

headermonths.map!{|word| word.mb_chars.capitalize.to_s}
#=> ["", "", "", "", "", "", "", "", "", "", ""]

Ruby 2.4

The code in your question will work just as expected with Ruby 2.4.

. " Unicode" .

+2

Below is an example of a reliable version with a capital letter that works in any ruby ​​starting from 1.9, but for Cyrillic only because of -32hardcoded.

NB : thanks and credits go to @Stefan and @EricDulusil, which lead me in the right direction

headermonths = %w|     
                      |
puts (headermonths.each do |s|
  s[0] = (s[0].ord - 32).chr(Encoding::UTF_8) 
end.inspect)
#⇒ ["", "", "", "", "", "",
#   "", "", "", "", ""]
+1
source

Source: https://habr.com/ru/post/1664680/


All Articles