Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to specify output file encoding in Ruby?

How can I set the encoding of a file when using a File#open?

like image 902
Fluffy Avatar asked Sep 04 '09 09:09

Fluffy


1 Answers

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").

like image 87
ire_and_curses Avatar answered Sep 23 '22 14:09

ire_and_curses