From: Yu Yu Date: 2011-08-20T07:11:46+09:00 Subject: Re: Searching in a directory 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. -- Posted via http://www.ruby-forum.com/.