netbeans 和命令窗口中的 Ruby 字符编码问题

发布于 2024-09-04 05:44:45 字数 1177 浏览 6 评论 0原文

我使用 netbeans 作为开发 IDE 并从 cmd 运行应用程序,但在 cmd 窗口中以及从 netbeans 运行应用程序时无法正确显示 ISO 8859-1 字符,例如 åäö

问题: 最佳实践是什么 就是

现在我要做的

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

获取 ä

我的环境

>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"

我在代码中的位置

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 修订版 26273)[i386-mingw32]

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]

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

疏忽 2024-09-11 05:44:45

如果要在源文件中使用非 ASCII 字符,则需要声明源文件的编码。源文件的第一行(或者第二行,如果你有 shebang 行)需要是一个注释行,它(大致)与正则表达式匹配

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

换句话说:注释行以一些内容开头,然后包含字符串coding 后跟一个可选符号,后跟一个有效的编码名称,后跟一些其他内容。

所以,如果你把它放在

# coding: ISO-8859-1

你的文件的顶部,你应该没问题。 等编辑器兼容。

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

请注意,正则表达式被明确设计为与 Vim或 Emacs

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

总是让我困惑的一件事是,Ruby 并不总是自动对字符串进行转码。您必须自己对它们进行转码:

puts 'ä'
# => ä

puts 'ä'.encode(
gt;.external_encoding || Encoding.default_external)
# => ä

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(
gt;.external_encoding || Encoding.default_external)
# => ä
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文