DZone Snippets is a public source code repository. Easily build up your personal collection of code snippets, categorize them with tags / keywords, and share them with the world

Snippets has posted 5883 posts at DZone. View Full User Profile

Pathname#empty?

11.06.2008
| 1938 views |
  • submit to reddit
        This should just be part of Pathname.

class Pathname
  # Returns true if the Pathname is a directory and contains no files or directories.
  def empty?
    self.directory? && self.entries.find_all {|f| f.to_s !~ /^\.\.?$/ }.empty? 
  end
end
    

Comments

Snippets Manager replied on Tue, 2008/11/04 - 2:11pm

Nice. There's always a cleaner way. I didn't think about counting.

Snippets Manager replied on Wed, 2007/05/16 - 10:08pm

(erm, of course that > should be a <=)

Snippets Manager replied on Wed, 2007/05/16 - 10:08pm

what about... def empty? directory? && entries.size > 2 end