Reputation: 53
Why
# encoding: utf-8
out=File.open("z\\test.txt", "a")
out.puts "ç"
out.close
out=File.open("z\\test.txt", "r")
puts out.read+"ś"
results in "incompatible character encodings: UTF-8 and CP852 (Encoding::CompatibilityError)"?
Upvotes: 2
Views: 4791
Reputation: 3654
This should explain a lot
# https://pl.wikipedia.org/wiki/Kodowanie_polskich_znak%C3%B3w
inp = "zale\xBFno\x9cci".force_encoding('Windows-1250')
# inp = File.open('content-win-1250.txt', :encoding => 'Windows-1250').read
inp = inp.encode('utf-8')
File.open("tmp.txt", "wb") do |out|
out.write(inp)
end
# file 'tmp.txt contains "zależności"
Upvotes: 0
Reputation: 88378
Your script works fine for me on my box.
Is the error coming from your terminal application or from Ruby?
My terminal app is set to use utf-8.
You can probably avoid this problem by explicitly supplying an encoding when opening your files. See http://www.ruby-doc.org/core/classes/File.html#M000069 and follow the links to IO::new.
Upvotes: 0
Reputation: 370112
The comment at the beginning of your ruby file, only determined the source encoding, i.e. it tells ruby which encoding the ruby file is encoded in. It does not tell it which encoding the files you're opening are encoded in - for that it still uses the system's default encoding unless you specifically request another one.
Apparently your system's default encoding is CP852, so if you want to open a file using utf-8, you'll have to specify that encoding when opening the file (passing the :encoding => "utf-8"
as an argument to File.open
).
Upvotes: 4