Misha Moroshko
Misha Moroshko

Reputation: 171321

Rails: How to downcase non-English string?

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.

Upvotes: 37

Views: 13605

Answers (5)

Jose Miguel Richard
Jose Miguel Richard

Reputation: 11

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

Upvotes: 1

mmichaa
mmichaa

Reputation: 760

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

Upvotes: 2

Dima Melnik
Dima Melnik

Reputation: 884

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

Upvotes: 6

Developer
Developer

Reputation: 1041

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

UnicodeUtils.downcase('Привет') #=> 'привет'

Upvotes: 7

fl00r
fl00r

Reputation: 83680

str = "Привет"
str.mb_chars.downcase.to_s
#=> "привет"

Upvotes: 97

Related Questions