Project

General

Profile

Feature #8269 ยป 8269_Find_each_file.patch

phluid61 (Matthew Kerwin), 04/22/2013 03:06 PM

View differences:

lib/find.rb
64 64
  end
65 65

  
66 66
  #
67
  # Recursively iterates over every file and directory listed as arguments,
68
  # calling the associated block with the name of every file encountered.
69
  #
70
  # Returns an enumerator if no block is given.
71
  #
72
  def each_file(*paths) # :yield: path
73
    block_given? or return enum_for(__method__, *paths)
74
    self.find(*paths){|f| yield f if File.file? f }
75
  end
76

  
77
  #
67 78
  # Skips the current file or directory, restarting the loop with the next
68 79
  # entry. If the current file is a directory, that directory will not be
69 80
  # recursively entered. Meaningful only within the block associated with
......
75 86
    throw :prune
76 87
  end
77 88

  
78
  module_function :find, :prune
89
  module_function :find, :each_file, :prune
79 90
end
test/test_find.rb
210 210
    }
211 211
  end
212 212

  
213
  def test_each_file
214
    Dir.mktmpdir {|d|
215
      File.open("#{d}/a", "w"){}
216
      Dir.mkdir("#{d}/b")
217
      File.open("#{d}/b/a", "w"){}
218
      File.open("#{d}/b/b", "w"){}
219
      Dir.mkdir("#{d}/c")
220
      a = []
221
      Find.each_file(d) {|f| a << f }
222
      assert_equal(["#{d}/a", "#{d}/b/a", "#{d}/b/b"], a)
223
    }
224
  end
225

  
226
  def test_each_file_enumerator
227
    Dir.mktmpdir {|d|
228
      File.open("#{d}/a", "w"){}
229
      Dir.mkdir("#{d}/b")
230
      File.open("#{d}/b/a", "w"){}
231
      File.open("#{d}/b/b", "w"){}
232
      Dir.mkdir("#{d}/c")
233
      e = Find.each_file(d)
234
      a = []
235
      e.each {|f| a << f }
236
      assert_equal(["#{d}/a", "#{d}/b/a", "#{d}/b/b"], a)
237
    }
238
  end
239

  
213 240
  class TestInclude < Test::Unit::TestCase
214 241
    include Find
215 242