Created
February 18, 2017 13:50
-
-
Save hakunin/ed95620adcc7ea610e0c490066f64975 to your computer and use it in GitHub Desktop.
Multi byte upcase_first polyfill
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class String | |
# polyfill until Rails 5, with support for multi-byte chars | |
# https://glaucocustodio.github.io/2016/05/19/rails-5-new-upcase-first-method/ | |
# https://coderwall.com/p/b74yda/rails-upcase-downcase-for-non-ascii-strings | |
unless method_defined? :upcase_first | |
def upcase_first | |
return self if length == 0 | |
self.mb_chars[0].upcase.mb_chars.concat(self[1..-1]).to_s | |
end | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment