[ruby-dev:39847] stable find.rb

From: Tanaka Akira <akr@...>
Date: 2009-12-06 14:47:35 UTC
List: ruby-dev #39847
ディレクトリを再帰的にたどった結果を比較することがあったのですが、
find.rb を使うと、yield されるファイル名の順序が readdir の挙動に依存して
不定なために使えなくて残念な思いをしました。

readdir が返す順に yield しなければならないという理由はないと思うので、
ディレクトリ内のファイルを sort してやるというのはどうでしょう?

% svn diff --diff-cmd diff -x '-u --ignore-all-space' lib/find.rb
Index: lib/find.rb
===================================================================
--- lib/find.rb	(???ӥ????? 26028)
+++ lib/find.rb	(???ȥ??ԡ?)
@@ -46,9 +46,9 @@
         end
 	begin
 	  if s.directory? then
-	    d = Dir.open(file)
-	    begin
-	      for f in d
+            fs = Dir.entries(file)
+            fs.sort!
+            fs.reverse_each {|f|
 		next if f == "." or f == ".."
 		if File::ALT_SEPARATOR and file =~ /^(?:[\/\\]|[A-Za-z]:[\/\\]?)$/ then
 		  f = file + f
@@ -58,10 +58,7 @@
 		  f = File.join(file, f)
 		end
 		paths.unshift f.untaint
-	      end
-	    ensure
-	      d.close
-	    end
+            }
 	  end
         rescue Errno::ENOENT, Errno::EACCES
 	end
-- 
[田中 哲][たなか あきら][Tanaka Akira]

In This Thread

Prev Next