37

How could I downcase a non-English string in Ruby on Rails 3 ?

str = "Привет"    # Russian 
puts str[0].ord   # => 1055
str.downcase!
puts str[0].ord   # => 1055 (Should be 1087)

I want it to work in Ruby 1.8.7 as well as Ruby 1.9.2.

Charles
  • 50,943
  • 13
  • 104
  • 142
Misha Moroshko
  • 166,356
  • 226
  • 505
  • 746

5 Answers5

97
str = "Привет"
str.mb_chars.downcase.to_s
#=> "привет"
fl00r
  • 82,987
  • 33
  • 217
  • 237
7

Why not to use gem unicode_utils. This gem will not force downcase to work, however you can use:

UnicodeUtils.downcase('Привет') #=> 'привет'
Nakilon
  • 34,866
  • 14
  • 107
  • 142
Developer
  • 983
  • 12
  • 12
6

If you want to use it easy like this:

> "Привет".downcase
=> "привет"

you have to put into initializers folder file string.rb

require 'unicode'

class String
  def downcase
    Unicode::downcase(self)
  end
  def downcase!
    self.replace downcase
  end
  def upcase
    Unicode::upcase(self)
  end
  def upcase!
    self.replace upcase
  end
  def capitalize
    Unicode::capitalize(self)
  end
  def capitalize!
    self.replace capitalize
  end
end
Dima Melnik
  • 862
  • 9
  • 23
2

Since Ruby 2.4 there is a built in full Unicode case mapping. Source: https://stackoverflow.com/a/38016153/888294. See Ruby 2.4.0 documentation for details: https://ruby-doc.org/core-2.4.0/String.html#method-i-downcase

mmichaa
  • 760
  • 6
  • 11
1

A nice and easy solution in rails is to add string.rb into initializers folder, then in this file you can override String using mb_chars, now downcase support accents and letters like Ñ

class String
  def downcase
    self.mb_chars.downcase.to_s
  end

  def capitalize
    self.mb_chars.capitalize.to_s
  end

  def upcase
    self.mb_chars.upcase.to_s
  end

  def titleize
    self.mb_chars.titleize.to_s
  end
end