[Ruby] Encoding when you get it on Windows + Ruby

1 minute read

First post

I think it’s a very small story, but I was lost for a long time with unexpected information. I will gradually participate in qiita from such a place.


A program that displays what you get with Windows 10 + Ruby 2.7.0.


str = gets.chomp
puts str

This is all, but when I entered Japanese with puts, the characters were garbled. I usually use VS Code and I wondered if it’s compatible with the console and Ruby, but I can’t use the command prompt. I was confused by the old (obsolete) notation such as ruby Ks while playing around with the encoding on the Ruby side, but in the end, it was displayed safely when I did the following item.



STDIN.set_encoding "Windows-31J"
str = gets.chomp
puts str.encode("Windows-31J", invalid: :replace, replace: ))

After all,

  • Windows console I/O encoding is Windows-31J
  • So, first, match the character code of Ruby’s standard input (first line)
  • However, if this is left as it is, a character that does not make sense will be attached at the beginning, so I will drop this in the description on the third line

That (I think).

  • After that, even if I passed a character string to eval etc. (of course if the original str made a grammatical meaning), it worked properly. ← I really wanted to do this

However, even this is not perfect, if the double-byte character is included in gets, the line break is not properly recognized and it becomes necessary to press the Enter key twice. I don’t know this workaround for now. Well, it’s not the behavior I’m worried about, so I’ll try to get over it for now.