Programming ≈ Fun

Written by Krešimir Bojčić

Downcase for UTF-8 Characters in Ruby

There is one gotcha in Ruby UTF-8 support.

String method downcase is local insensitive, meaning it ignores UTF-8 characters not in standard ASCII character set.

Example below will give you expected results:

"HELLO”.downcase

-->hello

Next example will not behave as desired as non standard characters remain in uppercase:

"DINING ROOM - SALLE À MANGER".downcase

-->dining room - salle À manger

It is supposed to be fixed in Ruby 2.0. Until then you’ll need some monkey patching:

gem install unicode
#enviroment.rb
require 'unicode'
require 'lib/string'
#lib/string.rb
class String
  def downcase
    Unicode::downcase(self)
  end
end

Risky business, I know. But it’s the only only solution that I know of.

Comments