Xavier Noria
11/6/2007 1:08:00 AM
On Nov 6, 2007, at 1:42 AM, Cong Chan wrote:
> I am writing a ruby script to insert some content into the beginning
> of
> some files.
>
> The files would be used in Windows or Mac, when I use File.puts method
> to write the file, it changes the line ending of the file.
>
> Any way to let ruby keep the previous line-ending of the file?
Not automatically.
If you know before hand that the line-ending convention is uniform you
can open the file in binary mode, pick a few bytes, and look for the
first line separator (=~ /\015?\012/).
Then write in binary mode and set $\ to that capture, aka the ouput
record separator. Use print instead of puts, since puts does not check
$\ but prints "\n" unconditionally.
-- fxn