Skip to content

Instantly share code, notes, and snippets.

@gbedardsice
Created October 21, 2012 19:06
Show Gist options
  • Save gbedardsice/3928112 to your computer and use it in GitHub Desktop.
Save gbedardsice/3928112 to your computer and use it in GitHub Desktop.
Verifying if a ruby string contains one or more accents
# encoding: utf-8
class String
# The extended characters map used by contains_accent. The accented characters
# are coded here using their numerical equivalent to sidestep encoding issues.
# These correspond to ISO-8859-1 encoding.
ACCENTS_MAPPING = {
'E' => [200,201,202,203],
'e' => [232,233,234,235],
'A' => [192,193,194,195,196,197],
'a' => [224,225,226,227,228,229,230],
'C' => [199],
'c' => [231],
'O' => [210,211,212,213,214,216],
'o' => [242,243,244,245,246,248],
'I' => [204,205,206,207],
'i' => [236,237,238,239],
'U' => [217,218,219,220],
'u' => [249,250,251,252],
'N' => [209],
'n' => [241],
'Y' => [221],
'y' => [253,255],
'AE' => [306],
'ae' => [346],
'OE' => [188],
'oe' => [189]
}
def contains_accent?
retval = false
str = String.new(self)
String::ACCENTS_MAPPING.each do |letter, accents|
packed = accents.pack('U*')
rxp = Regexp.new("[#{packed}]", nil)
retval = true if str.scan(rxp).size > 0
end
retval
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment