Hi. How can I set the encoding of a file when using a File#open?
+5
A:
Here's an example that outputs a file in the UTF-16LE encoding:
open("data.txt", "w:UTF-16LE")
Ruby looks at the encoding of the string you are writing, and transcodes as necessary. Here's a very detailed blog post describing mechanics with excellent examples (see the section called "The Default External and Internal Encodings").
ire_and_curses
2009-09-04 09:57:02
+1
A:
That blog also has a bunch of great information about character encoding with Ruby, including a post about encoding with Ruby 1.8.
Ryan Heneise
2009-09-24 14:53:35
A:
I need to write in ASCII code. Using File.open(filename,"w:ASCii") raise the error:
illegal access mode w:ASCII
Any ideas ?
Fabian
2010-07-30 18:39:43
If you only want the US character set of ASCII, use US-ASCII. If you just want raw octets (the full 0-255 range), then use ASCII-8BIT.
devyn
2010-08-06 09:03:15