Home > Mobile >  I need to convert 'jonathan-morón-16394669' into 'jonathan-morón-16394669'
I need to convert 'jonathan-morón-16394669' into 'jonathan-morón-16394669'

Time:11-05

I'm trying to solve an issue because sometimes data like 'jonathan-morón-16394669' gets saved and sometimes data like 'jonathan-morón-16394669' gets saved. So I need a smooth way to move around between those two versions of the string to be able to compare things and realize it's the same via matching them and just carry on without saving something twice in the DB.

I know I could just start using something like this but in reverse:

line = line.replace("é","e")
line = line.replace("ó","o")
line = line.replace("–","-")
line = line.replace("æ","e")
line = line.replace("è","e")
line = line.replace("à","e")
line = line.replace("á","i")
line = line.replace("ł","l")
line = line.replace("ń","n")
line = line.replace("ÿ", "y")
line = line.replace("þ", "p")
line = line.replace("ý", "y")
line = line.replace("ü", "u")
line = line.replace("û", "u")
line = line.replace("ú", "u")
line = line.replace("ù", "o")
line = line.replace("ö", "o")
line = line.replace("õ", "o")
line = line.replace("ô", "o")
line = line.replace("ó", "o")
line = line.replace("ò", "o")
line = line.replace("ñ", "n")
line = line.replace("ð", "e")
line = line.replace("ì", "i")
line = line.replace("í", "i")
line = line.replace("î", "i")
line = line.replace("ï", "i")
line = line.replace("Á","A")
line = line.replace("Â","A")
line = line.replace("Ã","A")
line = line.replace("Ä","A")
line = line.replace("Å","A")
line = line.replace("Æ","AE")
line = line.replace("Ç","C")
line = line.replace("È","E")
line = line.replace("É","E")
line = line.replace("Ê","E")
line = line.replace("Ë","E")
line = line.replace("Ì","I")
line = line.replace("Í","I")
line = line.replace("Î","I")
line = line.replace("Ï","I")
line = line.replace("Ð","D")
line = line.replace("Ñ","N")
line = line.replace("Ò","O")
line = line.replace("Ó","O")
line = line.replace("Ô","O")
line = line.replace("Õ","O")
line = line.replace("Ö","O")
line = line.replace("Ø","O")
line = line.replace("Ù","U")
line = line.replace("Ú","U")
line = line.replace("Û","U")
line = line.replace("Ü","U")
line = line.replace("Ý","Y")
line = line.replace("ß","B")
line = line.replace("à","a")
line = line.replace("á","a")
line = line.replace("â","a")
line = line.replace("ã","a")
...

But I need a more Rails way solution, elegant, simple

CodePudding user response:

You can use CGI::escape

CGI::escape('jonathan-morón-16394669') # => jonathan-morón-16394669

You could go also the other way around with CGI::unescape

CGI::unescape('jonathan-morón-16394669') # => jonathan-morón-16394669

CodePudding user response:

Thanks to razvans! The answer I needed was

URI.encode("jonathan-morón-16394669")

which outputs

[19] pry(main)> URI.encode("jonathan-morón-16394669")
=> "jonathan-morón-16394669"
  • Related