Windows CMD and international chars (åäö)

Hi all,

I have been pondering over a problem and I have been trying to find (if
there is any) a solution to it.

I am a Swedish user and I willingly admit that I am a newb with Ruby. I
am trying to create a console based db interface (sqlite) but I am
having problems printing Swedish characters (åäöÅÄÖ). When searching
around and reading up on the problem I seem to run into a dead end
wherever I turn. Solutions concerning this problem when using Rails is
fairly easy to find but so far I have found none when just scripting in
a windows console.

So my question to all you wise Ruby coders out there is this: Is there a
solution to my problem and if so what is it?

Thanks in advance.

/sweRascal

Andre Arvidsson wrote:

Hi all,

So my question to all you wise Ruby coders out there is this: Is there a
solution to my problem and if so what is it?

You have to set ‘Lucida Console’ as console font in window properties.
You may change console code page by ‘chcp’ command:

C:\XP\system32>chcp
Active code page: 1251

C:\XP\system32>chcp 65001
Active code page: 65001 <------- UTF-8

I use this:

encoding: utf-8

#Kernel::system(‘chcp 1251>nul’)

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

puts
“[#{Encoding.locale_charmap}::#{Encoding.default_external}|#{Encoding.default_internal}]”

puts ‘Превед!’

Andrey Kuznecov wrote:

You have to set ‘Lucida Console’ as console font in window properties.
You may change console code page by ‘chcp’ command:

C:\XP\system32>chcp 65001
Active code page: 65001 <------- UTF-8

But I know there is a bug:

C:\XP\system32>ruby -v
C:\users\Administrator\RubymineProjects\TestApp\main.rb
ruby 1.9.1p0 (2009-01-30 revision 21907) [i386-mswin32]

[CP65001::UTF-8|UTF-8]
Превед!ед!д!! <------- BUG

It must be ‘Превед!’ not ‘Превед!ед!д!!’

Andrey Kuznecov wrote:

C:\XP\system32>chcp 65001
Active code page: 65001 <------- UTF-8

In windows there are two locales: console (DOS VM) and system (OS) but
Ruby makes no difference.

Andrey Kuznecov wrote:

Andrey Kuznecov wrote:

C:\XP\system32>chcp 65001
Active code page: 65001 <------- UTF-8

In windows there are two locales: console (DOS VM) and system (OS) but
Ruby makes no difference.

Thanks alot!

I did a
system(‘chcp 1252>nul’)

at the start of the file and now åäöÅÄÖ print correctly…

Again thanks and amazing how quick I got an answer to the question…

/Andre

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

This solution works. The text is now correctly displayed in Windows
console.

But my 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. :frowning: 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.