Skip to content

Instantly share code, notes, and snippets.

@herval
Created October 27, 2009 16:49
Show Gist options
  • Save herval/219717 to your computer and use it in GitHub Desktop.
Save herval/219717 to your computer and use it in GitHub Desktop.
# String extension to remove non-ascii/graphic accent characters
# pt-BR: métodos para remover acentos (e espaços) de String
require 'iconv'
class String
# São José do Poço -> Sao Jose do Poco
def remove_non_ascii
Iconv.iconv("ASCII//IGNORE//TRANSLIT", "UTF-8", self).join.gsub(/[^a-z._0-9 -]/i, "").tr(".", "_")
end
# São José do Poço -> sao-jose-do-poco
# perfect for slugs!
def remove_non_ascii_and_spaces
# very compact, heh!?
remove_non_ascii.gsub(/(\s+)/, "_").dasherize.downcase
end
# São José do Poço -> saojosedopoco
def remove_non_ascii_and_spaces_and_dashes
remove_non_ascii.gsub(/(\s+)/, "").downcase
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment