How to specify output file encoding in Ruby?

前端 未结 3 1828
南旧
南旧 2020-12-15 03:04

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

3条回答
  •  心在旅途
    2020-12-15 03:46

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

提交回复
热议问题