1

In powershell(v2) condition,I run the command,it goes gibberish.

echo "すみません" | ruby -e "puts gets"

?????

enter image description here

My powershell is V2.How should I get the correct output?

Jason
  • 59
  • 4
  • 1
    What error code? – tadman Jan 03 '23 at 17:04
  • What are your Ruby and Windows versions? – Holger Just Jan 03 '23 at 17:28
  • As an aside: `?????` is the `ruby` call's _output string_, not an _error_. It is the lossy to-ASCII encoding performed by PowerShell on sending the data to Ruby that converts the unsupported-in-ASCII characters to literal `?` chars. (question marks). – mklement0 Jan 03 '23 at 18:44
  • output gibberish like "??????",I try to run: echo “すみません” |Ruby -e “put gets.force_encoding('UTF-8')”,but still output gibberish. – Jason Jan 04 '23 at 03:24

1 Answers1

2

As implied by the answer to your related question, you need to set $OutputEncoding / [Console]::OutputEncoding first to match the character encoding that ruby expects / outputs; e.g., for UTF-8:

# Make PowerShell both send and receive data as UTF-8 when talking to
# external (native) programs.
# Note: 
#  * In *PowerShell (Core) 7+*, $OutputEncoding *defaults* to UTF-8.
#  * You may want to save and restore the original settings.
$OutputEncoding = [Console]::OutputEncoding = [Text.UTF8Encoding]::new()

echo "すみません" | ruby -e "puts gets"

As for what you tried:

With Windows PowerShell using ASCII(!) encoding by default when sending data via the pipeline to external programs (see below), characters such as す can NOT be represented, and get "lossily" translated to literal ? characters.

Since none of the characters in your input string have an ASCII representation, the (ASCII representation of) verbatim string ????? was sent to ruby.


Background information:

In PowerShell it is the $OutputEncoding preference variable that determines the encoding used for sending data TO external programs, and in Windows PowerShell it defaults to ASCII(!), i.e. only characters in the 7-bit ASCII subrange of Unicode can be represented.

In PowerShell (Core) 7+, $OutputEncoding now more sensibly defaults to (BOM-less) UTF-8.

Unfortunately, with respect to receiving data from external programs, where [Console]::OutputEncoding matters for correct interpretation of the output, even PowerShell (Core) 7+ still uses the legacy OEM code page by default as of v7.3.1, which means that UTF-8 output from external programs is by default misinterpreted - see GitHub issue #7233 for a discussion.

mklement0
  • 382,024
  • 64
  • 607
  • 775