In Ruby, How to read UTF-8 from a socket?
ruby, sockets, utf-8
Solution
I believe `read_nonblock` uses `read`, which in turn says:
The resulted string is always ASCII-8BIT encoding.
Which means you don't need to specify `IO#set_encoding`, but that you can, after you read whole string, force its encoding (using `String#force_encoding!`) to `UTF-8`.
I emphasized 'whole', as you need to make sure that you read entire Unicode character at the end of the string, as if only part of it is read, you will get invalid UTF-8 character and Ruby might complain about it further down the line.
Problem
When a server sends UTF-8 bytes, how do you read them without characters becoming pure bytes? (\x40 etc)