使用JRuby在Windows上编写Unix换行符

Esk*_*ola 4 ruby unix windows newline jruby

我正在编写一个Ruby脚本来生成Unix shell脚本,但是我无法让JRuby在Windows上编写Unix换行符.

我写了一个test.rb包含以下内容的文件:

File.open("test.sh", 'w') do |f|
  f.write("#!/bin/sh\n")
  f.write("echo hello\n")
end
Run Code Online (Sandbox Code Playgroud)

当我使用命令执行它时,java -jar jruby-complete-1.6.5.jar test.rb生成的文件包含\r\n换行符而不是\n换行符.

如何强制JRuby用Unix换行编写文本文件?

Esk*_*ola 9

我设法通过在参数中添加"b"来修复它 File.open

File.open("test.sh", 'wb') do |f|
  f.write("#!/bin/sh\n")
  f.write("echo hello\n")
end
Run Code Online (Sandbox Code Playgroud)

IO类的文档说明如下:

Mode |  Meaning
-----+--------------------------------------------------------
 "b" |  Binary file mode (may appear with
     |  any of the key letters listed above).
     |  Suppresses EOL <-> CRLF conversion on Windows. And
     |  sets external encoding to ASCII-8BIT unless explicitly
     |  specified.
-----+--------------------------------------------------------
Run Code Online (Sandbox Code Playgroud)