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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T20:45:40+00:00 2026-05-23T20:45:40+00:00

I have this code: File.foreach(testfile.txt) { |line| print line } lines = File.readlines(testfile.txt) lines.each

  • 0

I have this code:

File.foreach("testfile.txt") { |line| print line }
lines = File.readlines("testfile.txt")
lines.each { |line| print line }

Ex. how could I edit this to get a specific line from testfile.txt ?

$text2 = gets("some code to give me text on line 2 from textfile.txt").chomp

So, I want to run a program that searches for 4 different variables that are editable by anyone. These will be in a separate file and thus the code will not have to be rewritten in order to change the test for a new set of search cases.

Say I have a document (I suppose text is easiest but am probably wrong) that looks like this:

Tea

Coffee

Hot Chocolate

Cookies

That will be all that is in the file. And another file(.rb) pulls these variables and searches for them in Google or whatever. If the code reads in bytes then I wouldn’t be able to change the variables to anything longer or shorter.

It would be feasible to have the document say

1:Tea

2:Coffee

3:Hot Chocolate

4:Cookies

So long as the code would only pull out the “Tea” or “coffee” and not the preceding number.

  • 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-23T20:45:41+00:00Added an answer on May 23, 2026 at 8:45 pm

    It sounds like you don’t want to return a line that matches a certain pattern, but you want to grab by line number.

    Solution 1:

    def read_line_number(filename, number)
      return nil if number < 1
      line = File.readlines(filename)[number-1]
      line ? line.chomp : nil
    end
    

    Solution 2a – a bit more efficient because it doesn’t try to read entire file:

    require 'english'
    def read_line_number_1a(filename, number)
      found_line = nil
      File.foreach(filename) do |line|
        if $INPUT_LINE_NUMBER == number
          found_line = line.chomp
          break
        end
      end
      found_line
    end
    

    Solution 2b – also efficient, but in a more functional style (though I haven’t checked whether detect will read to end-of-file)

    def read_line_number(filename, match_num)
      found_line, _idx =
        File.enum_for(:foreach, filename).each_with_index.detect do |_cur_line, idx|
          cur_line_num = idx+1
          cur_line_num == match_num
        end
      found_line.chomp
    end
    

    Usage:

    text = read_line_number("testfile.txt", 2)
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have this code below which works ok ish. $swearWords = file(blacklist.txt); foreach ($swearWords
I have this code which will include template.php file from inside each of these
Here I have this code: <tbody data-bind=foreach: entries> <tr> <td><i class=icon-file></i> <a href=# data-bind=text:
I have this code for parsing a CSV file. var query = from line
I have this php code: $lines = file(data.csv); $nested = array(); $links = array();
I have problem with this code: file = tempfile.TemporaryFile(mode='wrb') file.write(base64.b64decode(data)) file.flush() os.fsync(file) # file.seek(0)
I have this code in a xaml file now in a event handler code
I have this code to download a file, but on sourceforge.net sever there is
I have this code to move my uploaded file to a specific directory: if
I have this code that reads from XML file. It gets five strings (groupId,

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.