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 3966482
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 20, 20262026-05-20T03:36:24+00:00 2026-05-20T03:36:24+00:00

I tried searching for this, but couldn’t find much. It seems like something that’s

  • 0

I tried searching for this, but couldn’t find much. It seems like something that’s probably been asked before (many times?), so I apologize if that’s the case.

I was wondering what the fastest way to parse certain parts of a file in Ruby would be. For example, suppose I know the information I want for a particular function is between lines 500 and 600 of, say, a 1000 line file. (obviously this kind of question is geared toward much large files, I’m just using those smaller numbers for the sake of example), since I know it won’t be in the first half, is there a quick way of disregarding that information?

Currently I’m using something along the lines of:

while  buffer = file_in.gets and file_in.lineno <600
  next unless file_in.lineno > 500
  if buffer.chomp!.include? some_string
    do_func_whatever
  end
end

It works, but I just can’t help but think it could work better.

I’m very new to Ruby and am interested in learning new ways of doing things in it.

  • 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-20T03:36:25+00:00Added an answer on May 20, 2026 at 3:36 am
    file.lines.drop(500).take(100) # will get you lines 501-600
    

    Generally, you can’t avoid reading file from the start until the line you are interested in, as each line can be of different length. The one thing you can avoid, though, is loading whole file into a big array. Just read line by line, counting, and discard them until you reach what you look for. Pretty much like your own example. You can just make it more Rubyish.

    PS. the Tin Man’s comment made me do some experimenting. While I didn’t find any reason why would drop load whole file, there is indeed a problem: drop returns the rest of the file in an array. Here’s a way this could be avoided:

    file.lines.select.with_index{|l,i| (501..600) === i}
    

    PS2: Doh, above code, while not making a huge array, iterates through the whole file, even the lines below 600. 🙁 Here’s a third version:

    enum = file.lines
    500.times{enum.next} # skip 500
    enum.take(100) # take the next 100
    

    or, if you prefer FP:

    file.lines.tap{|enum| 500.times{enum.next}}.take(100)
    

    Anyway, the good point of this monologue is that you can learn multiple ways to iterate a file. 😉

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

Sidebar

Related Questions

I've tried searching for this but couldn't find examples that suited my situation. I
I tried searching for this but couldn't find a similar question. I have a
I'm not entirely sure if this is possible and tried searching but couldn't find
Tried to do some searching on this, but couldn't find exactly what I needed.
I've been searching this forum up and down but I couldn't find what I
I tried searching around, but I couldn't find anything that would help me out.
I've tried searching around but couldn't find a solution to this. I am running
I have tried searching for this but couldn't really find a proper answer. I
I've tried searching through search engines,MSDN,etc. but can't anything. Sorry if this has been
I've been searching for a solution for this problem but I couldn't solve it!

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.