# Line ending problem

**URL:** <https://www.ruby-forum.com/t/line-ending-problem/119326>\
**Category:** Ruby\
**Created:** [November 6, 2007, 1:41am UTC](https://www.ruby-forum.com/t/line-ending-problem/119326 "2007-11-06T01:41:11Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![goodness](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/g/7993a0/32.png) [@goodness](https://www.ruby-forum.com/u/goodness)\
**Post date:** [November 6, 2007, 1:41am UTC](https://www.ruby-forum.com/t/line-ending-problem/119326/1 "2007-11-06T01:41:11Z")

</div>

Hi

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?

Thanks

---

<div class="post-metadata">

**Author:** ![goodness](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/g/7993a0/32.png) [@goodness](https://www.ruby-forum.com/u/goodness)\
**Post date:** [November 6, 2007, 2:03am UTC](https://www.ruby-forum.com/t/line-ending-problem/119326/2 "2007-11-06T02:03:08Z")

</div>

Cong Chan wrote:

> Hi
> 
> I am writing a ruby script to insert some content into the beginning of  
> some files.

> Thanks

So you want to add stuff to the beginning of a file? Well, as far as I  
know, you can’t just add stuff to the beginning, you have to create a  
new file, add your stuff, then append the old stuff at the end of the  
new file.

new\_file = File.new(“your\_new\_file.txt”, “w+”)

new\_file.write(“I’m at the beginning\n”)

IO.readlines(“your\_old\_file.txt”).each do |line|  
new\_file.write(line)  
end

new\_file.close

~Jeremy

---

<div class="post-metadata">

**Author:** ![goodness](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/g/7993a0/32.png) [@goodness](https://www.ruby-forum.com/u/goodness)\
**Post date:** [November 6, 2007, 2:29am UTC](https://www.ruby-forum.com/t/line-ending-problem/119326/3 "2007-11-06T02:29:38Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![goodness](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/g/7993a0/32.png) [@goodness](https://www.ruby-forum.com/u/goodness)\
**Post date:** [November 6, 2007, 4:19pm UTC](https://www.ruby-forum.com/t/line-ending-problem/119326/4 "2007-11-06T16:19:13Z")

</div>

On 11/5/07, Xavier N. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

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

I’d suggest that rather than this you use write instead of print or  
puts and put the line breaks in explicitly.

My concern over using $\ is that it’s global so it affects any other  
Io objects, and I’m not sure it’s a thread-safe global.

–  
Rick DeNatale

My blog on Ruby  
[http://talklikeaduck.denhaven2.com/](http://talklikeaduck.denhaven2.com/)
