# Newbie: add few lines to file ..?

**URL:** <https://www.ruby-forum.com/t/newbie-add-few-lines-to-file/127699>\
**Category:** Ruby\
**Created:** [January 31, 2008, 2:36pm UTC](https://www.ruby-forum.com/t/newbie-add-few-lines-to-file/127699 "2008-01-31T14:36:07Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![newman](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/n/3bc359/32.png) [@newman](https://www.ruby-forum.com/u/newman)\
**Post date:** [January 31, 2008, 2:36pm UTC](https://www.ruby-forum.com/t/newbie-add-few-lines-to-file/127699/1 "2008-01-31T14:36:07Z")

</div>

Hi!

I try to write code which adding extra lines to my file

ruby -e 'readlines.each{… } ’ myfile\_name~ \> myfile\_name

How to make it. I should use readlines…?

---

<div class="post-metadata">

**Author:** ![newman](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/n/3bc359/32.png) [@newman](https://www.ruby-forum.com/u/newman)\
**Post date:** [January 31, 2008, 3:31pm UTC](https://www.ruby-forum.com/t/newbie-add-few-lines-to-file/127699/2 "2008-01-31T15:31:09Z")

</div>

newman wrote:

> Hi!
> 
> I try to write code which adding extra lines to my file

File.open(‘data.txt’, ‘a’) do |file|  
file.puts(‘hello world’)  
end

---

<div class="post-metadata">

**Author:** ![newman](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/n/3bc359/32.png) [@newman](https://www.ruby-forum.com/u/newman)\
**Post date:** [January 31, 2008, 3:50pm UTC](https://www.ruby-forum.com/t/newbie-add-few-lines-to-file/127699/3 "2008-01-31T15:50:45Z")

</div>

Yeah, as 7stud says, open the file with the append flag…

You should look through the docs of the File class, and possibly  
Google? They’re suprisingly useful…

Regards,  
Lee

---

<div class="post-metadata">

**Author:** ![newman](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/n/3bc359/32.png) [@newman](https://www.ruby-forum.com/u/newman)\
**Post date:** [January 31, 2008, 3:55pm UTC](https://www.ruby-forum.com/t/newbie-add-few-lines-to-file/127699/4 "2008-01-31T15:55:30Z")

</div>

newman wrote:

> I try to write code which adding extra lines to my file
> 
> ruby -e 'readlines.each{… } ’ myfile\_name~ \> myfile\_name

Replace the \> with a \>\> and the output of your script will be appended  
to  
myfile\_name instead of replacing it.

HTH,  
Sebastian
