Forum: Ruby Windows CMD and international chars (åäö)

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
F931054c6043054504b5905909908ef3?d=identicon&s=25 Andre Arvidsson (swerascal)
on 2009-04-20 08:13
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
702331b25e2fb4c75f3ca0adc74be1b5?d=identicon&s=25 Andrey Kuznecov (dr_dizel)
on 2009-04-20 09:30
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 'Превед!'
702331b25e2fb4c75f3ca0adc74be1b5?d=identicon&s=25 Andrey Kuznecov (dr_dizel)
on 2009-04-20 09:47
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 'Превед!ед!д!!'
702331b25e2fb4c75f3ca0adc74be1b5?d=identicon&s=25 Andrey Kuznecov (dr_dizel)
on 2009-04-20 10:06
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.
F931054c6043054504b5905909908ef3?d=identicon&s=25 Andre Arvidsson (swerascal)
on 2009-04-20 10:31
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
D40e419b14bfe393b07206602ae0271a?d=identicon&s=25 Andrey Mikhaylov (lolmaus)
on 2011-04-24 20:20
``````
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. :( 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.
This topic is locked and can not be replied to.