Skip to content

Instantly share code, notes, and snippets.

@hakunin
Created February 18, 2017 13:50
Show Gist options
  • Save hakunin/ed95620adcc7ea610e0c490066f64975 to your computer and use it in GitHub Desktop.
Save hakunin/ed95620adcc7ea610e0c490066f64975 to your computer and use it in GitHub Desktop.
Multi byte upcase_first polyfill
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