Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Ruby > strange behaviors in writing/saving file

Reply
Thread Tools

strange behaviors in writing/saving file

 
 
Li Chen
Guest
Posts: n/a
 
      09-12-2008
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.
--
Posted via http://www.ruby-forum.com/.

 
Reply With Quote
 
 
 
 
Patrick Doyle
Guest
Posts: n/a
 
      09-12-2008
On Fri, Sep 12, 2008 at 8:16 AM, Li Chen <(E-Mail Removed)> wrote:
>
> ##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.


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

 
Reply With Quote
 
 
 
 
Antonin Amand
Guest
Posts: n/a
 
      09-12-2008
What's the class of e.inner_text ? Did you try f.write ?

 
Reply With Quote
 
Li Chen
Guest
Posts: n/a
 
      09-12-2008
Antonin Amand 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


--
Posted via http://www.ruby-forum.com/.

 
Reply With Quote
 
Li Chen
Guest
Posts: n/a
 
      09-12-2008
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



--
Posted via http://www.ruby-forum.com/.

 
Reply With Quote
 
Patrick Doyle
Guest
Posts: n/a
 
      09-12-2008
On Fri, Sep 12, 2008 at 9:10 AM, Li Chen <(E-Mail Removed)> 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

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Strange behaviors of Iterator for set Bo Yang C++ 6 11-20-2008 08:57 AM
std_logic_vector entry as hexadecimal : Different behaviors Sylvain Munaut VHDL 2 12-22-2004 12:19 AM
DHTML Behaviors and ASP.NET Scupper ASP .Net 1 05-28-2004 07:50 PM
Behaviors in Behaviors Mark Kremers Javascript 0 07-31-2003 09:45 AM
Help: Usercontrol - different behaviors based on parent VB Programmer ASP .Net 3 07-01-2003 06:01 PM



Advertisments