【问题标题】:How to check the content of each .txt file in a folder with Ruby如何使用 Ruby 检查文件夹中每个 .txt 文件的内容
【发布时间】:2014-02-23 11:15:51
【问题描述】:

我有一个包含文件的文件夹。我想知道如果文件夹中的每个 .txt 文件都包含“BREAK”一词,我该如何检查它。我知道这一定很容易,但我有点怀念完成它的方式。

这是我迄今为止尝试过的

Dir.glob('/path/to/dir/*.txt') do |txt_file|
  # And here I need a method that opens the 'txt_file'
  # and checks if it contains "BREAK"
end

【问题讨论】:

    标签: ruby file-io io


    【解决方案1】:

    下面将返回包含“BREAK”的文件的array

    files = Dir.glob('/path/to/dir/*.txt').select do |txt_file|
      File.read(txt_file).include? "BREAK"
    end
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-09-10
      • 1970-01-01
      • 1970-01-01
      • 2012-07-27
      相关资源
      最近更新 更多