My Unix Ruby program needs to write a file that will be read by
SqlServer running on Windows. I need for the lines I write to this
file to end in \r\n
, the DOS/Windows line ending, rather than \n
,
the Unix line ending. I want this to happen without me having to
manually add the \r to the end of each line.
If my program writes to the file like this:
File.open("/tmp/foo", "w") do |file|
file.puts "stuff"
end
Then the file has Unix line endings:
$ od -c foo
0000000 s t u f f \n
That is expected, since my program is running on Unix. But I need for this file (and this file only) to have DOS line endings.
If I add the \r to each line manually:
File.open("/tmp/foo", "w") do |file|
file.puts "stuff\r"
end
Then the file has DOS line endings:
$ od -c /tmp/foo
0000000 s t u f f \r \n
This works, but has to be repeated for each line I want to write.
As shown by this SO answer, I can modify the string using String#encode before writing it:
File.open("/tmp/foo", "w") do |file|
file.puts "alpha\nbeta\n".encode(crlf_newline: true)
end
This results in DOS line endings:
$ od -c /tmp/foo
0000000 a l p h a \r \n b e t a \r \n
This has the advantage that if I am writing multiple lines at once, one call to #encode will change all the line endings for that one write. However, it's verbose, and I still have to specify the line ending for every write.
How can I cause each puts
to an open file in Unix to end
the line in the Windows \r\n
line ending rather than the Unix '\n'
line ending?
I am running Ruby 2.3.1.
To write your file in this way, while you have the file open, go to the Edit menu, select the "EOL Conversion" submenu, and from the options that come up select "UNIX/OSX Format". The next time you save the file, its line endings will, all going well, be saved with UNIX-style line endings.
DOS uses carriage return and line feed ("\r\n") as a line ending, which Unix uses just line feed ("\n"). You need to be careful about transferring files between Windows machines and Unix machines to make sure the line endings are translated properly.
There's an option for this, the same one you're using on encode
also works with File.open
:
File.open('/tmp/foo', mode: 'w', crlf_newline: true) do |file|
file.puts("alpha")
file.puts("beta")
end
This option not only ends lines in \r\n
, it also translates any explicit \n
into \r\n
. So this:
file.puts("alpha\nbar")
will write alpha\r\nbar\r\b
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With