0

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

1 Answer 1

2

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.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.