From: ara.t.howard@... Date: 2006-10-17T12:53:36+09:00 Subject: Re: Test if Directory is Empty On Tue, 17 Oct 2006, John Turner wrote: not sure what you mean there - that's the correct usage... read the ri for catch. > Also, won't the "Dir.glob("#{ d }/.*")" return [".", ".."] (well, pass "." > to the block) and break this? > > How about: > > def isEmpty?(dir) > Dir.glob("#{dir}/*", File::FNM_DOTMATCH) do |f| > return false unless f =~ /\.\.?/ > end > return true > end yes. that's definitely better... that regex is deadly though: harp:~ > irb irb(main):001:0> "this is a file ..." =~ /\.\.?/ ? true : false => true refining some of the ideas in the thread: harp:~ > cat a.rb class Dir def empty? Dir.glob("#{ path }/*", File::FNM_DOTMATCH) do |e| return false unless %w( . .. ).include?(File::basename(e)) end return true end def self.empty? path new(path).empty? end end d = Dir.new '/tmp' p d.empty? require 'fileutils' FileUtils.rm_f 'empty' FileUtils.mkdir_p 'empty' p(Dir.empty?('empty')) harp:~ > ruby a.rb false true now we can begin the discussion on how to use such a method - since nearly any code that did would have a race condition! ;-) -a -- my religion is very simple. my religion is kindness. -- the dalai lama