1

I have the following string with UTF-8 encoding:

string = "C\u00EAlaV\u00EDta"

string.encoding
# => #<Encoding:UTF-8>

puts string
# => CêlaVíta

I'd like to remove all special characters and transform this into:

celavita

The reason I want to do this is to use it in a url. I keep getting URI::InvalidURIError from HTTParty.

I'm hoping there's a universal method I can also use on other strings.

Any ideas?

== Edit:

Based on Uri Agassi's duplicate mark, I've tried the following without success:

require "i18n"

string = "C\u00EAlaV\u00EDta"

I18n.transliterate(string)
# =>
# [deprecated] I18n.enforce_available_locales will default to true in the future. If you really want to skip validation of your locale you can set I18n.enforce_available_locales = false to avoid this message.
# ArgumentError: invalid byte sequence in UTF-8
# from /Users/paron/.rbenv/versions/2.1.2/lib/ruby/gems/2.1.0/gems/i18n-0.6.11/lib/i18n/backend/transliterator.rb:79:in `gsub'
pts
  • 71,941
  • 19
  • 102
  • 171
paron
  • 53
  • 4

0 Answers0