From: zdennis Date: 2006-03-22T09:15:18+09:00 Subject: Re: Find.find and files in cwd rtilley wrote: > zdennis wrote: > >> To get only normal files try: >> Dir["*"].delete_if{ |e| not File.file?( e ) } >> >> To get only directories try: >> Dir["*"].delete_if{ |e| not File.directory?( e ) } >> >> To get only links try: >> Dir["*"].delete_if{ |e| not File.link?( e ) } >> >> Zach > > > The whole thing seems a bit hackish to me and it still leaves links to > files and links to folders on my Windows test machine. > > I like this: > contents = Dir.entries(Pathname.getwd) > > Better than this: > contents = Dir["#{Dir.getwd}/*"] > > It makes more sense to me and seems more readable. > > I wish that with either approach File.file? would work like this: > > Dir.entries(Pathname.getwd).each do |entry| > if File.file?(entry) > puts entry > end > end > Working with file globs is *not* hackish in my opinion. File globs are a powerful and wonderful thing. See Dir#glob for more information. Granted reject is better use then delete_if in this scenario. I like Logan's last solution, although to tidy it up: Dir.entries( Dir.pwd ).reject{ |f| File.directory?( f ) } Zach