Rails: Upcase/downcase for Non-ASCII Strings
Introduction
Upcase/downcase method for ASCII strings works pretty good.
"turtle".upcase
=> "TURTLE"
"TURTLE".downcase
=> "turtle"
Problem
=
Polish translation of "turtle" is "żółw". Let's try to upcase it:
"żółw".upcase
=> "żółW"
What about downcase?
"ŻÓŁW".downcase
=> "ŻÓŁw"
As we can see, upcase/downcase method doesn't work properly for string containing non-ASCII characters.
Solution
Rails give us helpful method - mb_chars.
"żółw".mb_chars.upcase
=> "ŻÓŁW"
"ŻÓŁW".mb_chars.downcase
=> "żółw"
Now, it works!
Click here to read more about mb_chars.
Written by Sebastian Muszyński
Related protips
Have a fresh tip? Share with Coderwall community!
Post
Post a tip
Best
#Rails
Authors
Sponsored by #native_company# — Learn More
#native_title#
#native_desc#