I am trying to save a file, with the below code, in different encoding, for example ANSI.
How can I do this, on Windows?
file=File.new("c:/ruby/hps.txt","w")
file.puts 'there is some text'
file.close
There's an example in the Encoding documentation about how to do this. open, File.new, and IO.new all take the same mode and encoding arguments.
Using File.open with a block is safer and more succinct than managing the filehandle yourself.
File.open("c:/ruby/hps.txt", "w:ISO-8859-1") do |f|
f.write("some text")
end
This example writes the string as ISO 8859-1 aka Windows-1252 aka Latin-1.
"ANSI" encoding is ambiguous. Check Encoding.list for what's available.