FrozenHeart
FrozenHeart

Reputation: 20746

How to determine the encoding of the string in Ruby

I have a string received via the gets.chomp of the TcpSocket class. How can I determine the encoding of this string? Then I need to convert it to the Windows console encoding so I can print it. Is there any built-in way or third-party library to do it in Ruby?

Thanks in advance.

Upvotes: 12

Views: 8496

Answers (1)

Arup Rakshit
Arup Rakshit

Reputation: 118261

  • How can I determine the encoding of this string?

Use this method #encoding- Returns the Encoding object that represents the encoding of obj.

  • Then I need to convert it to the Windows console encoding so I can print it...

Take look at the class Encoding to get the ideas, about how to convert to required encodings.

Upvotes: 15

Related Questions