Forum: Ruby Problems with opening and reading data from a file

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
Cf25fbf53c67e27d95845e77e949b56f?d=identicon&s=25 Stuart Clarke (sclarke)
on 2008-12-07 12:53
I am trying to read keywords and phrases from a .txt file and run these
words against a given directory structure. I seem to be having a few
problems, when I run my code i get the following error:

C:\Documents and Settings\user\Desktop>keywordsearch.rb "C:\Documents
and Sett
ings\user"
C:/Documents and Settings/user/Desktop/keywordsearch.rb:21:in
`initialize': No
 such file or directory - C:\Documents and
Settings\user\Desktop\terms.txt (Er
rno::ENOENT)

I cannot see what is wrong with my code see below:

File.open(curPath) do |file|
        file.each do |line|
          o = File.open("C:\\Documents and
Settings\\user\\Desktop\\terms.txt")
          terms = o.readlines
          terms.each do |term|
            if line =~ term
              puts "#{curPath}"
            end
          end
          o.close
        end
      end

Any ideas, I really appreciate the guidance.

Regards
703fbc991fd63e0e1db54dca9ea31b53?d=identicon&s=25 Robert Dober (Guest)
on 2008-12-07 14:26
(Received via mailing list)
On Sun, Dec 7, 2008 at 12:46 PM, Stuart Clarke
<stuart.clarke1986@gmail.com> wrote:
> Settings\user\Desktop\terms.txt (Er
>            if line =~ term
> --
> Posted via http://www.ruby-forum.com/.
>
>
I am not sure \\ works, try simple /.
And I am sure the file exists and is readable.
Another topic
instead of
o = File.open
...
o.close
you might prefer
File.open do |o|
  ...
end
which will assure o is closed *even* if an exception is raised in your
block.

HTH
R.


--
Il computer non è una macchina intelligente che aiuta le persone
stupide, anzi, è una macchina stupida che funziona solo nelle mani
delle persone intelligenti.
Computers are not smart to help stupid people, rather they are stupid
and will work only if taken care of by smart people.

Umberto Eco
This topic is locked and can not be replied to.