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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 4, 20262026-06-04T07:59:36+00:00 2026-06-04T07:59:36+00:00

Given a hash that contains function names like find_by_user, find_by_id, … I want to

  • 0

Given a hash that contains function names like “find_by_user”, “find_by_id”, …

I want to search in a directory of files, and return a object that has each file name, along with the line numbers of where the function name occurred.

I have this so far:

files = Dir.glob(@folder_path)

files.each do |file_name|
  content = File.read(file_name)

end

This will be scanning a few hundred files.

  • 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-06-04T07:59:37+00:00Added an answer on June 4, 2026 at 7:59 am

    Here’s the basic functionality you need:

    # Given a path to a file and a regex,
    # return an array of paired filename+line number matches
    def matching_lines( file_path, regex )
      name = File.basename(file_path)
      File.readlines(file_path)
        .map.with_index{ |line,i| [name,line,i] }
        .select{ |name,line,i| line =~ regex }
        .map{ |name,line,i| [name,i] }
    end
    

    You can choose to use this as you like, iterating over multiple files and/or patterns, or using Regexp.union to create a pattern matching any one of a set of strings.


    However: this is what grep was made for:

    C:\>grep --line-number Nokogiri *.rb
    push_nav_to_docs.rb:13: nav_dom = Nokogiri.XML(IO.read(NAV))
    push_nav_to_docs.rb:39:         landing = Nokogiri.XML(html)
    push_nav_to_docs.rb:53: doc = Nokogiri.XML(IO.read(doc_path))
    push_nav_to_docs.rb:73: if File.exists?(toc_path) && toc = Nokogiri.XML(IO.read(toc_path)).at('ul')
    push_nav_to_docs.rb:104:        container << Nokogiri.make("<ul/>").tap do |ul|
    

    In Ruby you could call this code and get the output you want via:

    lookfor = "Nokogiri"
    grepped = `grep --line-number #{lookfor} *.rb`
    results = grepped.scan(/^(.+?):(\d+)/)
    #=> [["push_nav_to_docs.rb", "13"], ["push_nav_to_docs.rb", "39"], ["push_nav_to_docs.rb", "53"], ["push_nav_to_docs.rb", "73"], ["push_nav_to_docs.rb", "104"]]
    

    Grep can also recurse into directories, match only particular file names, take regular expressions as patterns, and more.

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

Sidebar

Related Questions

I'm writing a library that contains some hash functions. I want one of the
Given this hash that comes though :params positions=>{ 1=>{x=>50, y=>30}, 2=>{x=>22, y=>53}, 3=>{x=>68, y=>35}
I've been looking for a way to hash a given string in C# that
This is an assignment that I've been given, it says that if an hash-set
I wanted to understand how the SHA0 hash function was broken. I understand that
Given a string how to create a unique identifier / hash for that string
In C# Given a hash table like {id:'1', name:'foo'} How can I dynamically create
I need a function that, given a salt integer and a value integer will
This question is the inverse of this question . Given a hash that has
I want to push a reference to a hash. By that I mean 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.