So that, for example, the мама мыла раму became the Мама Мыла Раму , and санкт-петербург became Санкт-Петербург .

    3 answers 3

    Starting with Ruby 2.4.0, you can:

     'санкт-петербург'.gsub(/\b./, &:upcase) 

    In earlier versions, using the Unicode heme:

     'санкт-петербург'.gsub(/[А-я]+/) { |word| Unicode::capitalize(word) } 

    In Rails (or using the active_support gem ), you can do this:

     require 'active_support/core_ext/string/multibyte' 'санкт-петербург'.mb_chars.titleize.to_s 

      Ruby does not work well with Cyrillic. So much so that in Rails it is sometimes easier to do this at the database level, than to do transformations with a caliber like yours.

      In cases where this should be done in Ruby, your decision to beat is very difficult - you need to use either the Unicode gem already mentioned or the add-ons above it. Therefore, your decision is probably the best.

      ActiveSupport (parts of Rails , which in theory can be used separately) has the mb_chars method, which makes the syntax a bit more familiar with a proxy class (or decorator). It looks like this:

       'санкт-петербург'.gsub(/[А-я]+/) { |word| word.mb_chars.capitalize } 

      Under the hood, this is all the same Unicode gem (see ActiveSupport::Multibyte::Chars ), and ActiveSupport is often criticized for its abundance of monkeypatch . Although you can only tighten this method with:

       require 'active_support/core_ext/string/multibyte' 
      • In Rails, you can probably do this: 'санкт-петербург'.mb_chars.titleize.to_s - installero
      • @installero, I forgot about this helper. He's from the same place, I suppose. Have you checked this option? - D-side
      • [2] pry(main)> 'санкт-петербург'.mb_chars.titleize.to_s => "Санкт-Петербург" - installero
      • @installero bravo, I'm wrong: D Write the second answer X) - D-side

      If you do not consider the Cyrillic alphabet, then this:

       s.gsub(/\b./, &:upcase) 
      • And if you consider? - installero
      • one
        Then this is a question that should be asked separately. - Nakilon
      • one
        Corrected the title - installero
      • Correct the title after two days - do not labor. - Nakilon