# Strange behaviors in writing/saving file

**URL:** <https://www.ruby-forum.com/t/strange-behaviors-in-writing-saving-file/148395>\
**Category:** Ruby\
**Created:** [September 12, 2008, 2:23pm UTC](https://www.ruby-forum.com/t/strange-behaviors-in-writing-saving-file/148395 "2008-09-12T14:23:27Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![alex-osu3](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/13edae/32.png) [@alex-osu3](https://www.ruby-forum.com/u/alex-osu3)\
**Post date:** [September 12, 2008, 2:23pm UTC](https://www.ruby-forum.com/t/strange-behaviors-in-writing-saving-file/148395/1 "2008-09-12T14:23:27Z")

</div>

Hi all,

I query a website,extract some info from fetched page, and save/write  
the info in txt format. My current problem: I can print all the info to  
the screen but only the last line is written/saved to the file. I wonder  
what is going on.

Thanks,

Li

##here is the code line to print the results to the screen and save to  
the file  
File.open(‘test.txt’,‘w’){|f| puts e.inner\_text; f.puts e.inner\_text}

##screen results:  
abacus  
(n.)  
A manual computing device consisting of a frame holding parallel rods  
strung with movable counters.  
abacus  
(n.)  
A slab on the top of the capital of a column.

## written/saved file

A slab on the top of the capital of a column.

---

<div class="post-metadata">

**Author:** ![alex-osu3](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/13edae/32.png) [@alex-osu3](https://www.ruby-forum.com/u/alex-osu3)\
**Post date:** [September 12, 2008, 2:37pm UTC](https://www.ruby-forum.com/t/strange-behaviors-in-writing-saving-file/148395/2 "2008-09-12T14:37:08Z")

</div>

On Fri, Sep 12, 2008 at 8:16 AM, Li Chen [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> abacus  
> (n.)  
> A slab on the top of the capital of a column.
> 
> ## written/saved file
> 
> A slab on the top of the capital of a column.

I am guessing from the context that the code you posted is in some  
sort of loop that fetches a line of text from the website. Perhaps  
something like

while (e.inner\_text = gets)  
File.open(‘test.txt’,‘w’){|f| puts e.inner\_text; f.puts e.inner\_text}  
end

In this case, each time through your loop, you open “test.txt”,  
destroying whatever it contained before, write one line, and then  
close the file.

If you want to append to the end of the file each time you open it,  
you will need to change ‘w’ to ‘a’, in your call to File#open.

–wpd

---

<div class="post-metadata">

**Author:** ![alex-osu3](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/13edae/32.png) [@alex-osu3](https://www.ruby-forum.com/u/alex-osu3)\
**Post date:** [September 12, 2008, 2:38pm UTC](https://www.ruby-forum.com/t/strange-behaviors-in-writing-saving-file/148395/3 "2008-09-12T14:38:19Z")

</div>

What’s the class of e.inner\_text ? Did you try f.write ?

---

<div class="post-metadata">

**Author:** ![alex-osu3](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/13edae/32.png) [@alex-osu3](https://www.ruby-forum.com/u/alex-osu3)\
**Post date:** [September 12, 2008, 3:14pm UTC](https://www.ruby-forum.com/t/strange-behaviors-in-writing-saving-file/148395/4 "2008-09-12T15:14:19Z")

</div>

Antonin A. wrote:

> What’s the class of e.inner\_text ? Did you try f.write ?

1. the class of e.inner\_text is String.
2. the problem is sovled by changine mode ‘w’ to mode ‘a’
3. either f.write or f.puts saves the last line in mode ‘w’  
but they work well in mode ‘a’

Li

---

<div class="post-metadata">

**Author:** ![alex-osu3](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/13edae/32.png) [@alex-osu3](https://www.ruby-forum.com/u/alex-osu3)\
**Post date:** [September 12, 2008, 5:17pm UTC](https://www.ruby-forum.com/t/strange-behaviors-in-writing-saving-file/148395/5 "2008-09-12T17:17:16Z")

</div>

On Fri, Sep 12, 2008 at 9:10 AM, Li Chen [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> Hi Patrick,
> 
> Thanks for the input.
> 
> Now the problem is sovled by changing the mode ‘w’ to ‘a’ as following:
> 
> File.open(‘test.txt’,‘a’){|f| puts e.inner\_text; f.puts e.inner\_text}

Just be aware that text will be appended to the file forever. You  
might want to do something at the top of your script to remove or  
empty the file, if you don’t want to be confused by past results.

–wpd

---

<div class="post-metadata">

**Author:** ![alex-osu3](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/13edae/32.png) [@alex-osu3](https://www.ruby-forum.com/u/alex-osu3)\
**Post date:** [September 12, 2008, 3:17pm UTC](https://www.ruby-forum.com/t/strange-behaviors-in-writing-saving-file/148395/6 "2008-09-12T15:17:44Z")

</div>

Hi Patrick,

Thanks for the input.

Now the problem is sovled by changing the mode ‘w’ to ‘a’ as following:

File.open(‘test.txt’,‘a’){|f| puts e.inner\_text; f.puts e.inner\_text}

Li
