Skip to content

Instantly share code, notes, and snippets.

@hsribei
Created December 20, 2010 17:17
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save hsribei/748665 to your computer and use it in GitHub Desktop.
Save hsribei/748665 to your computer and use it in GitHub Desktop.
downcase with accents for latin1 (sort of)
# -*- coding: utf-8 -*-
class String
def downcase_with_accents
return nil if s.nil?
norm = self.downcase
norm.tr!('ÁÉÍÓÚÇ', 'aeiouç')
norm.tr!('ÀÈÌÒÙ', 'aeiou')
norm.tr!('ÄËÏÖÜ', 'aeiou')
norm.tr!('ÂÊÎÔÛ', 'aeiou')
norm.tr!('áéíóú', 'aeiou')
norm.tr!('àèìòù', 'aeiou')
norm.tr!('äëïöü', 'aeiou')
norm.tr!('âêîôû', 'aeiou')
norm
end mapping = {
'Á' => 'á',
'É' => 'é',
'Í' => 'í',
'Ó' => 'ó',
'Ú' => 'ú',
'À' => 'à',
'Â' => 'â',
'Ê' => 'ê',
'Ô' => 'ô',
'Ã' => 'ã',
'Õ' => 'õ',
'Ç' => 'ç'
}
mapping.inject(self.downcase_without_accents) { |str, m|
str.tr(m[0], m[1])
}
end
alias :downcase_without_accents :downcase
alias :downcase :downcase_with_accents
def capitalize_with_accents
self[0..0] + self[1..-1].downcase
end
alias :capitalize_without_accents :capitalize
alias :capitalize :capitalize_with_accents
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment