title: How can I find a substring in multiple files?
body: <p>I need to use Ruby to go over directory and look for text in specific type files.</p>
  
  <p>For example, I have folder with these files: <em>a.txt, b.txt, c.trr, d.trr</em>
  and I want my code to check only <em>c.trr</em> and <em>d.trr</em> files and find if one of them contains the string "aaa".</p>
  
  <p>I tried some options but non of them seems to work for me.</p>
  
  <p>I tried:</p>
  
  <pre><code>Dir.foreach("/myusername/Desktop/a") do |fname|
      if  (File.extname(fname) == ".trr")
         begin
          fname.each_line do |line|
         if 
          line.scan(Regexp.new('aaa'))
          puts "found it!"
         else
          puts "no can do sir"
          end
          end
          rescue Exception =&gt; ex
                  puts ex
          end
      end
  end
  </code></pre>
  
  <p>Didn't work.
  and when I tried to print <code>line</code> variable I got as a response the name of the file and not the lines in it.</p>
  
  <p>I tried to change the <code>scan</code> function to <code>match</code>, and I also tried to 
  change <code>Regexp.new('aaa')</code> to <code>\aaa\</code>.</p>
  
  <p>Non of them seems to work. 
  Does <code>Dir.foreach</code> also opens the file? If it is not, how can I open it? 
  I tried to use <code>File.open(fname)</code> but I got the following exception:</p>
  
  <blockquote>
    <p>No such file or directory @ rb_sysopen</p>
  </blockquote>