Searching in a directory

Unfortunately without seeing what you have so far there's no way for me to pinpoint the issue.

Regards,
Chris White
http://www.twitter.com/cwgem

···

On Aug 19, 2011, at 3:11 PM, Yu Yu wrote:

Chris White wrote in post #1017580:

On Aug 19, 2011, at 1:51 PM, Yu Yu wrote:
dir_path = './test_files'
str = "dog"

Dir.chdir(dir_path) do

Dir.glob("*.txt").inject(false) do |found, filename|
  IO.foreach(filename) do |line|
    if line =~ /#{str}/
      puts "#{filename}: #{str}"
      if found == true
       puts "Second instance found"
       exit # or however you want to handle this
      else
       found = true
      end
    end
  end
  found
end

end

This exits as soon as the second match is found, but you could have it
run a method or something like that instead.

Sorry, but there's still a problem with the code.
If I put a method instead of exit nothing happens. If I put a method
into the else-branch I only call the method if the string is double.

Remember: If the string dog is found in file2 the search should stop /
call a method.