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

  • Home
  • SEARCH
  • 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 6710365
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T08:00:47+00:00 2026-05-26T08:00:47+00:00

Hi just getting into Ruby, and I am trying to learn some basic file

  • 0

Hi just getting into Ruby, and I am trying to learn some basic file reading commands, and I haven’t found any solid sources yet.

I am trying to go through certain lines from that file, til the end of the file.

So in the file where it says FILE_SOURCES I want to read all the sources til end of file, and place them in a file.

I found printing the whole file, and replacing words in the file, but I just want to read certain parts in 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-26T08:00:48+00:00Added an answer on May 26, 2026 at 8:00 am

    I would do it like this (assuming you can read the entire file into memory):

    source_lines = IO.readlines('source_file.txt')
    start_line   = source_lines.index{ |line| line =~ /SOURCE_LINE/ } + 1
    File.open( 'other_file.txt', 'w' ) do |f|
      f << source_lines[ start_line..-1 ].join( "\n" )
    end
    

    Relevant methods:

    • IO.readlines to read the lines into an array
    • Array#index to find the index of the first line matching a regular expression
    • File.open to create a new file on disk (and automatically close it when done)
    • Array#[] to get the subset of lines from the index to the end

    If you can’t read the entire file into memory, then I’d do a simpler variation on @tadman’s state-based one:

    started = false
    File.open( 'other_file.txt', 'w' ) do |output|
      IO.foreach( 'source_file.txt' ) do |line|
        if started then
          output << line
        elsif line =~ /FILE_SOURCES/
          started = true
        end
      end
    end
    

    Welcome to Ruby!

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm just getting into creating some WCF services, but I have a requirement to
I'm just getting into unit testing, and have written some short tests to check
Well just getting into the flow of thing with Python. Reading a few books,
I'm just getting into Facebook application development. I've got some questions about how the
I am just getting into SQL Server, and I have some experience with MS
I'm just getting into C#, so forgive me if this is a basic question.
I'm just getting into RSpec and playing around a bit with some easy examples
I'm just getting into Ruby - one of the things I'm having a little
Just getting into SQL stored queries right now... anyway, here's my database schema (simplified
I am just getting into ibatis with SqlMap for the first time and I

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.