# Encoding issue under Windows

**URL:** <https://rubytalk.org/t/encoding-issue-under-windows/62522>\
**Category:** ruby-talk\
**Created:** [24 April 2011 18:06 UTC](https://rubytalk.org/t/encoding-issue-under-windows/62522 "2011-04-24T18:06:49Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![Andrey\_Mikhaylov](https://avatars.discourse-cdn.com/v4/letter/a/e9bcb4/32.png) [@Andrey\_Mikhaylov](https://rubytalk.org/u/Andrey_Mikhaylov)\
**Post date:** [24 April 2011 18:06 UTC](https://rubytalk.org/t/encoding-issue-under-windows/62522/1 "2011-04-24T18:06:49Z")

</div>

Windows console works with the CP866 encoding.

So input and output should be redefined to CP866.

This thread [http://www.ruby-forum.com/topic/184730](http://www.ruby-forum.com/topic/184730) suggests a nice  
solution:

```auto
Encoding.default_external = Encoding.find(Encoding.locale_charmap)
Encoding.default_internal = __ENCODING__

[STDIN, STDOUT, STDERR].each do |io|
  io.set_encoding(Encoding.default_external, Encoding.default_internal)
end

```

And it works. The text is now correctly displayed in Windows console.

But the code crashes whenever Ruby faces a string that contains a  
non-CP866 character (like em dash "—"):

in `write': U+2014 from UTF-8 to IBM866  
(Encoding::UndefinedConversionError)

How do i overcome this error? Not using UTF-8 is not an option.

Iconv has a nice solution: add "//IGNORE" to encoding name and error no  
longer takes place. But IO does not recognize "//IGNORE", so this  
solution i unacceptable.

I can't use Iconv instead of IO due to the following reason. I use  
RubyMine. Unlike Windows console, RubyMine console works in pure UTF-8.  
But whenever code run by RubyMine writes to disk, it seems to use  
Windows encoding because text gets written in corrupt encoding. ☹ And i  
do not know a way to distinguish RubyMine from Windows console so that  
my program could perfrom only the conversions necessary for the current  
environment.

> **···**
>
> --  
> Posted via [http://www.ruby-forum.com/](http://www.ruby-forum.com/).

---

<div class="post-metadata">

**Author:** ![Roger\_Pack4](https://avatars.discourse-cdn.com/v4/letter/r/bb73d2/32.png) [@Roger\_Pack4](https://rubytalk.org/u/Roger_Pack4)\
**Post date:** [25 April 2011 21:54 UTC](https://rubytalk.org/t/encoding-issue-under-windows/62522/2 "2011-04-25T21:54:29Z")

</div>

I'd ping core on this one.

> **···**
>
> --  
> Posted via [http://www.ruby-forum.com/](http://www.ruby-forum.com/).
