How to open a file and search for a word?

后端 未结 4 1308
温柔的废话
温柔的废话 2021-02-02 00:28

How can I open a file and search for a word inside it using Ruby?

4条回答
  •  心在旅途
    2021-02-02 01:21

    Something like this might help:

    def word_exists_in_file
       f = File.open("your_file.txt") #opens the file for reading
       f.each do line
          print line
          if line.match /your_word_to_match/
             return true
          end
       end
       false
    end
    

提交回复
热议问题