0

I have developed a system where a specific user has access to search for a specific file. The problem is that this file is in a folder so I am having a hard time coding it.

My method must be able to find my keyword input in the file name.

For example, If I search for Bob, I will get all the files where Bob are included in the filename

def search_user(search)


            keyword = File.readlines('test3.yml')  


            matches = keyword.select { |username| username[/#{search}/] } 


            if File.read("test3.yml").include?(search) 
                puts "_____________________________________________"            
                puts ("Search results for student: " + search + ":") #
          puts
                puts matches
                puts "_____________________________________________"
                else #If not it will give the user feedback that its not there
                puts "_____________________________________________"
          puts
                puts ("Sorry, we couldnt find #{search} in the system.") 
                puts "_____________________________________________"
            end
    end
tadman
  • 208,517
  • 23
  • 234
  • 262
  • 1
    Do you even need to build this in ruby? You can just use `grep`: `grep -r keyword path/to/folder` – Tom Lord Dec 07 '17 at 16:46
  • yes, we need to build all this in ruby... that is one of the criteria unfortunately – Andy Klingser Dec 07 '17 at 16:52
  • Possible duplicate of [Searching a folder and all of its subfolders for files of a certain type](https://stackoverflow.com/questions/3498539/searching-a-folder-and-all-of-its-subfolders-for-files-of-a-certain-type) – Tom Lord Dec 07 '17 at 16:55
  • `Dir.glob("**/*#{search}*")` should do basically this. It's probably insecure to interpolate user content in directly, FWIW. – Chris Heald Dec 07 '17 at 17:20
  • Chris Heald, Were should I input Dir.glob("**/*#{search}*") ? – Andy Klingser Dec 07 '17 at 17:34

0 Answers0