strange behaviors in writing/saving file

Discussion in 'Ruby' started by Li Chen, Sep 12, 2008.

  1. Li Chen

    Li Chen Guest

    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/.
     
    Li Chen, Sep 12, 2008
    #1
    1. Advertising

  2. On Fri, Sep 12, 2008 at 8:16 AM, Li Chen <> 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
     
    Patrick Doyle, Sep 12, 2008
    #2
    1. Advertising

  3. What's the class of e.inner_text ? Did you try f.write ?
     
    Antonin Amand, Sep 12, 2008
    #3
  4. Li Chen

    Li Chen Guest

    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/.
     
    Li Chen, Sep 12, 2008
    #4
  5. Li Chen

    Li Chen Guest

    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/.
     
    Li Chen, Sep 12, 2008
    #5
  6. On Fri, Sep 12, 2008 at 9:10 AM, Li Chen <> 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
     
    Patrick Doyle, Sep 12, 2008
    #6
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Sylvain Munaut
    Replies:
    2
    Views:
    2,167
    Sylvain Munaut
    Dec 22, 2004
  2. VB Programmer
    Replies:
    3
    Views:
    365
    William F. Robertson, Jr.
    Jul 1, 2003
  3. Luis Esteban Valencia
    Replies:
    0
    Views:
    2,520
    Luis Esteban Valencia
    Jan 6, 2005
  4. Bo Yang
    Replies:
    6
    Views:
    331
    James Kanze
    Nov 20, 2008
  5. Mark Kremers

    Behaviors in Behaviors

    Mark Kremers, Jul 31, 2003, in forum: Javascript
    Replies:
    0
    Views:
    119
    Mark Kremers
    Jul 31, 2003
Loading...

Share This Page