Reputation: 193
we import a file and in the file there for special crater at first I try encoding methods and decoding ruby and nothing happens, so I used gsub.
work it moist as the character group Ì©
and ̤
For the rest no problem replaces me.
here method replace
def replace_chars(name)
chars = {
"Ž" => 'é',
"Â" => "ç",
"‘" => "ë",
"â„¢" => "ô",
"̤" => "ç",
"Ì©" => "é",
"•" => "ï"
}
puts "before #{name}"
chars.each do |key,value|
name.gsub!(key,value)
end
puts "after #{name}"
end
if I but my method
replace_chars('̤liver Žponime')
here puts the output of the method, the first he has not succeeded in changing the word but in the second he has made the change.
output : after ÃŒç¤liver éponime
I do not understand why he does not want to take my character ̤
and Ì©
.
Upvotes: 0
Views: 160
Reputation: 29318
Here is another solution that will avoid the need to iterate all the characters and will replace matches. @Prashant4020 is correct that you will need to order the keys in descending length or at least sort them by length before performing this operation as Â
right now will match and substitute prior to ̤
.
def replace_chars(name
chars = {
"̤" => "ç",
"Ì©" => "é",
"•" => "ï",
"â„¢" => "ô",
"Ž" => 'é',
"‘" => "ë",
"Â" => "ç"
}
#name.gsub!(/#{chars.keys.join('|')}/,chars)
#as suggested by @steenslag Regexp::union is definitely less of a hack
name.gsub!(Regexp.union(chars.keys),chars)
#translates to name.gsub!(/̤|Ì©"|•|â„¢|Ž|‘|Â/,{"̤" => "ç","Ì©"=>"é","•"=>"ï","â„¢"=>"ô","Ž"=>'é',"‘"=>"ë","Â"=>"ç"})
end
This creates a regex that will match the keys from the chars
hash and then use the values to substitute the keys. This way gsub!
will not be called for keys that do not exist in the name
at all.
Upvotes: 2
Reputation: 1601
Replace your code with this:
def replace_chars(name)
chars = {
"̤" => "ç",
"Ì©" => "é",
"•" => "ï",
"â„¢" => "ô",
"Ž" => 'é',
"‘" => "ë",
"Â" => "ç"
}
puts "before #{name}"
chars.each do |key,value|
name.gsub!(key,value)
end
puts "after #{name}"
end
replace_chars('̤liver Žponime')
before ̤liver Žponime
after çliver éponime
Upvotes: 0