Rails has support for working with Unicode characters using the ActiveSupport multibyte chars functionality.
For example:
string = "Çafé"
string.mb_chars.downcase.to_s
=> "çafé"Unicode and Chars in ActiveSupport had their own implementation for
methods like downcase, upcase, swapcase, normalize, etc.
Rails 6 deprecated below methods in ActiveSupport in favor of using String methods directly.
-
Unicode#downcase/upcase/swapcase in favor of
String#downcase/upcase/swapcase. -
Unicode#normalize, Chars#normalize in favor of
String#unicode_normalize. -
Chars.consumes? in favor of
String#is_utf8?.
These String methods are available from Ruby 2.3+ onwards and hence they are delegated to Ruby Strings.
Before Rails 6
pry(main)> ActiveSupport::Multibyte::Unicode.downcase("TEST")
=> "test"
pry(main)> ActiveSupport::Multibyte::Unicode.normalize("\u007A")
=> "z"
pry(main)> ActiveSupport::Multibyte::Chars.consumes?("Is this UTF-8 encoded?")
=> trueIn Rails 6
Executing the above methods in Rails 6 will issue DEPRECATION warning
as seen below:
pry(main)> ActiveSupport::Multibyte::Unicode.downcase("TEST")
DEPRECATION WARNING: ActiveSupport::Multibyte::Unicode#downcase
is deprecated and will be removed from Rails 6.1.
Use String methods directly.
=> "test"
pry(main)> ActiveSupport::Multibyte::Unicode.normalize("\u007A")
DEPRECATION WARNING: ActiveSupport::Multibyte::Unicode#normalize
is deprecated and will be removed from Rails 6.1.
Use String#unicode_normalize(:nfkc) instead.
=> "z"
pry(main)> ActiveSupport::Multibyte::Chars.consumes?("Is this UTF-8 encoded?")
DEPRECATION WARNING: ActiveSupport::Multibyte::Chars.consumes?
is deprecated and will be removed from Rails 6.1.
Use string.is_utf8? instead.
=> true