views:

58

answers:

1

I use netbeans as development IDE and runs the application from cmd but have problems to display ISO 8859-1 characters like åäö correct in both cmd window and when I run the application from netbeans

Question: What is best practice to set it up

Right now I do

@output.puts indent + "V" + 132.chr + "lkommen till Ruby Camping!"

to get ä

My environment

>chcp 65001

Active code page: 65001

>ruby main.rb

Source encoding: <Encoding:US-ASCII>
Default external: #<Encoding:UTF-8>
Default internal: nil
Locale charmap: "CP65001"

where I have in the code

def self.printEncoding
    puts "Source encoding: #{__ENCODING__.inspect}" if defined? __ENCODING__
    if defined? Environment::Encoding
      puts "Default external: #{Encoding.default_external.inspect}"
      puts "Default internal: #{Encoding.default_internal.inspect}"
      puts "Locale charmap: #{ Encoding.locale_charmap.inspect}"
    end
    puts "LANG environment variable: #{ENV['LANG'].inspect}" unless ENV['LANG'].nil?
  end

>ruby -v ruby 1.9.1p378 (2010-01-10 revision 26273) [i386-mingw32]

+2  A: 

If you want to use non-ASCII characters in your source file, you need to declare the encoding of the source file. The very first line of the source file (or the second, if you have a shebang line) needs to be a comment line which (roughly) matches the regexp

#.*?coding[:=]?\s+(.*)(?:\s+.*)?

In other words: a comment line which starts with some stuff, then contains the string coding followed by an optional symbol followed by a valid encoding name followed by some other stuff.

So, if you put

# coding: ISO-8859-1

at the top of your file, you should be okay. Note that the regexp is explicitly designed to be compatible with editors such as Vim:

# vim: fileencoding=UTF-8 ft=ruby syn=ruby ts=2 sw=2 ai eol et si

Or Emacs:

# *-* mode: ruby; coding: utf-8; tab-width: 2; indent-tabs-mode: nil *-*

One thing that always trips me up, is that Ruby does not always automatically transcode strings. You have to transcode them yourself:

puts 'ä'
# => ä

puts 'ä'.encode($>.external_encoding || Encoding.default_external)
# => ä
Jörg W Mittag
Thanks but no success found a could link on the subject and maybe it is netbeans or the cmd window that is the problemhttp://blog.grayproductions.net/categories/character_encodingsMy result right now http://content.screencast.com/users/salgo60/folders/Snagit/media/4e320726-1ecf-4f68-9ea9-21db0d82ee00/06.06.2010-12.51.15.png
salgo60
@salgo60: Did you see my edit? I thought that Ruby did the transcoding automatically, but apparently that is not the case. You have to transcode the string yourself, before printing it. (Actually, according to my own intuition as well as James Edward Gray II's articles, you should *not* have to do that: strings should get converted to the IO object's (`$>` in this case) external encoding automatically. Something is not quite right about my understanding of Ruby 1.9 m17n.)
Jörg W Mittag
Thanksit works in the command window where I run Ruby 1.9but still netbeans seems give me some "unexpected characters"Source encoding: #<Encoding:ISO-8859-1>Default external: #<Encoding:UTF-8>Default internal: nilLocale charmap: "CP850" Välkommen till Ruby Camping!I am satiesfied with this....
salgo60