Search code examples
rubyhttp-redirectencodingioerror-code

Ruby IO redirection to a file by using Kernel.puts,How can I ensure that the stored file's encoding type is ANSI or UTF-8


In windows: visual studio code IDE,I wrote the command like:

ruby -E UTF-8 -e "puts '汤姆,Welcome to my home'" > test.txt

,then I use the command:

ruby -E UTF-8 -e "puts gets" < test.txt

,but error codes occurred when reading.Like:

��ķ,Welcome to my home.

enter image description here

Finally I found the "test.txt" file's encoding type is unicode.

If I insist on using IO redirection to a file and choose kernel.puts, how can I ensure that the stored file's encoding type is UTF-8?

What should I do to ensure that the file encoding type after redirection is UTF-8?Please help me.


Solution

  • Your screenshot implies that you're using cmd.exe (Command Prompt), the legacy Windows shell.[1]

    By default, it uses the system's active legacy OEM code page, which is typically a fixed 8-bit character encoding limited to 256 characters, i.e. a single-byte encoding, e.g. Code Page 437 on US-English systems, as reported when you run chcp.

    If you want cmd.exe to use UTF-8 encoding instead, run chcp 65001 first.


    For character-encoding considerations with respect to PowerShell, cmd.exe's modern successor, see this answer to your related later question.


    [1] This is implied by two facts: the prompt string in your screenshots looks something like C:\path> and has no space after it (unlike in PowerShell, where it is also preceded by PS ) and you're successfully using < for input redirection, which, between cmd.exe and PowerShell, only cmd.exe supports (in PowerShell it causes an error).