Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7691061
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T20:32:21+00:00 2026-05-31T20:32:21+00:00

The Ruby script i am writing is going to be run every morning and

  • 0

The Ruby script i am writing is going to be run every morning and will pull information about backup files and write them to a csv file. This file has column names on the first line.

I have gotten it to work by appending to the end of the file:

open("#{curDir}/Backup_Times.csv", 'a') do |f|

        ...
end

I would like to see the newest data first without having to sort in Excel.

In Ruby, is there a way to write this data starting at the 2nd line of the file?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-31T20:32:22+00:00Added an answer on May 31, 2026 at 8:32 pm

    You write a new file, applying your change at the desired line, then rename result back to the original file name. The following method will copy the file and yield the output file object to a block at the correct line, so that block can output your new lines.

    def insert_lines_following_line file, line_no
      tmp_fn = "#{file}.tmp"
      File.open( tmp_fn, 'w' ) do |outf|
        line_ct = 0
        IO.foreach(file) do |line|
          outf.print line
          yield(outf) if line_no == (line_ct += 1)
        end
      end
      File.rename tmp_fn, file
    end
    
    insert_lines_following_line( "#{curDir}/Backup_Times.csv", 1 ) do |outf|
      # output new csv lines in this block
      outf.puts ['foo','bar','baz',1,2,3].join(",") # or however you build your csv line
    end
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am writing a small Ruby script that will run in a CLI. To
I'm writing a Ruby script and want behaviour like this: every 15 seconds do
I am about to be writing a Ruby on Rails app which will use
I am writing a Ruby script that will generate a large flat HTML menu
I'm writing a ruby bootstrapping script for a school project, and part of this
I'm writing a script that makes some trivial changes and then commits them to
I'm writing a Ruby script to generates a Unix shell script, but I'm unable
I am in the process of writing a Ruby script/app that helps me compiling
I'm writing a Ruby script and would like to use a n-ary tree data
I am writing a simple ruby script. I want a help message to be

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.