# Newbie: add few lines to file ..?

**URL:** <https://rubytalk.org/t/newbie-add-few-lines-to-file/44160>\
**Category:** ruby-talk\
**Created:** [31 January 2008 13:35 UTC](https://rubytalk.org/t/newbie-add-few-lines-to-file/44160 "2008-01-31T13:35:03Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![newman](https://avatars.discourse-cdn.com/v4/letter/n/3bc359/32.png) [@newman](https://rubytalk.org/u/newman)\
**Post date:** [31 January 2008 13:35 UTC](https://rubytalk.org/t/newbie-add-few-lines-to-file/44160/1 "2008-01-31T13:35:03Z")

</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:** ![7stud](https://avatars.discourse-cdn.com/v4/letter/7/57b2e6/32.png) [@7stud](https://rubytalk.org/u/7stud)\
**Post date:** [31 January 2008 14:31 UTC](https://rubytalk.org/t/newbie-add-few-lines-to-file/44160/2 "2008-01-31T14:31:02Z")

</div>

newman wrote:

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

File.open('data.txt', 'a') do |file|  
&nbsp;&nbsp;file.puts('hello world')  
end

> **···**
>
> --  
> Posted via [http://www.ruby-forum.com/\](http://www.ruby-forum.com/%5C).

---

<div class="post-metadata">

**Author:** ![Sebastian\_Hungereck1](https://avatars.discourse-cdn.com/v4/letter/s/3ab097/32.png) [@Sebastian\_Hungereck1](https://rubytalk.org/u/Sebastian_Hungereck1)\
**Post date:** [31 January 2008 14:54 UTC](https://rubytalk.org/t/newbie-add-few-lines-to-file/44160/3 "2008-01-31T14:54:53Z")

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

> **···**
>
> --  
> Jabber: sepp2k@jabber.org  
> ICQ: 205544826

---

<div class="post-metadata">

**Author:** ![Lee\_Jarvis3](https://avatars.discourse-cdn.com/v4/letter/l/7ea924/32.png) [@Lee\_Jarvis3](https://rubytalk.org/u/Lee_Jarvis3)\
**Post date:** [31 January 2008 14:50 UTC](https://rubytalk.org/t/newbie-add-few-lines-to-file/44160/4 "2008-01-31T14:50:04Z")

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