Hello everybody,
I’d like to get a hint about a little Ruby problem I’m dealing with.
In my spare time I wrote a little Ruby program with a plain text
interface for I/O. I use Windows XP (yeah, I know with Ruby 1.8.6.
Now for the problem: In Germany, we have letters called “Umlaute”,
which are rarely used in the English language. An example you might be
familiar with is the name ot the rock band “Motörhead”. (No, I don’t
know their music, it’s just an example.)
Now, if I wanted to save a file with the single line “Motörhead”, I
would want to do something like this:
irb(main):001:0> a_string = “Motörhead”
=> “Mot\224rhead”
irb(main):002:0> a_file = File.new(“test.txt”, “w”)
=> #<File:test.txt>
irb(main):003:0> a_file << a_string
=> #<File:test.txt>
irb(main):004:0> a_file.close
=> nil
irb(main):005:0>
However, when I open “test.txt” with a text editor, I get this result:
Mot"rhead
When I read a file with the correct spelling…
irb(main):006:0> puts File.open(“test.txt”, “r”).readlines
…I get this result:
Mot÷rhead
=> nil
I’ve tried to use Iconv, but I don’t know the names of the character
sets I have to use. To solve the problem, I’ve written this little
script…
def umlaute (line, replace_dict = File_To_Shell)
changed_line = line.clone
replace_dict.each_pair {|from, to| changed_line.sub!(from, to)}
changed_line
end
…which uses two hashes in order to replace the “offending”
characters back and forth:
File_To_Shell = {
/\344/ => “\204”, #
“ä” /\366/ => “\224”, #
“ö” /\374/ => “\201”, #
“ü” /\304/ => “\216”, # “Ä”
/\326/ => “\231”, # “Ö”
/\334/ => “\232”, # “Ü”
/\337/ => “\341” # “ß”
}
Shell_To_File = {
/\204/ => “\344”, #
“ä” /\224/ => “\366”, #
“ö” /\201/ => “\374”, #
“ü” /\216/ => “\304”, # “Ä”
/\231/ => “\326”, # “Ö”
/\232/ => “\334”, # “Ü”
/\341/ => “\337” # “ß”
}
This seems to work, but I consider this a rather ugly workaround and
hardly a decent solution, since it forces me to type…
a_string = umlaute(“Motörhead”)
…or even…
puts umlaute(“Motörhead”)
…whenever I have a “strange” character or two in my strings. Any help
is appreciated.
Thanks in advance,
Rainer Wolf