Reputation: 1822
In another question it was asked how to replace Umlaute. The accepted question was the following code:
# encoding: utf-8
foo = "ich bin doch nicht blöd, mann!".gsub(/[äöü]/) do |match|
case match
when "ä" 'ae'
when "ö" 'oe'
when "ü" 'ue'
end
end
puts foo
However, when I try to run this, the output is:
$ ruby /tmp/test.rb
ich bin doch nicht bld, mann!
So the Umlaute obviously don't get replaced. Is there something I am missing? I'm using Ruby 1.9.3p362 (2012-12-25 revision 38607) [x86_64-linux]
Upvotes: 1
Views: 921
Reputation: 80065
(Not a real answer to the question, but a bit large for a comment.) gsub
has syntax for this kind of substitution, using a Hash:
#encoding: utf-8
table = {"ä" => 'ae',
"ö" => 'oe',
"ü" => 'ue'}
re = Regexp.union(table.keys)
# re = /[äöü]/ # is fine too
p "ich bin doch nicht blöd, mann!".gsub(re, table)
# => "ich bin doch nicht bloed, mann!"
Upvotes: 2
Reputation: 140210
You are using incorrect syntax, you either need to use then
or a newline and indentation.
# encoding: utf-8
foo = "ich bin doch nicht blöd, mann!".gsub(/[äöü]/) do |match|
case match
when "ä" then 'ae'
when "ö" then 'oe'
when "ü" then 'ue'
end
end
puts foo
or
# encoding: utf-8
foo = "ich bin doch nicht blöd, mann!".gsub(/[äöü]/) do |match|
case match
when "ä"
"ae"
when "ö"
"oe"
when "ü"
"ue"
end
end
The robust way to do this would be result = Iconv.iconv('ascii//ignore//translit', 'utf-8', foo)
but you need to set locale to "de_DE"
which cannot be done in ruby without a c extension.
Upvotes: 7
Reputation: 575
"ich bin doch nicht blöd, mann!".gsub("ä","ae").gsub("ö","oe").gsub("ü","ue")
Should do the trick
Upvotes: 2