From: Caleb Clausen Date: 2010-08-24T01:57:50+09:00 Subject: Re: directory listing without . and .. On 8/23/10, Ryan Davis wrote: > On Aug 22, 2010, at 23:40 , Rajarshi Chakravarty wrote: >> Hi, >> How can I get an array of all legitimate sub directories in the current >> directory? >> >> path = "D:/Traffic" >> Dir.chdir(path) >> >> This gives 2 extra entries "." and ".." >> Dir.open(stationDir).entries.reject{|f| File.file?(f)} > > In all my years of ruby I don't think I've ever used Dir.open(...).entries. > I almost _always_ use Dir.glob (aka Dir[...]). I also almost always use the > block form for chdir. > > Dir.chdir("/Users/ryan") do > p Dir[File.join("Work", "*")] > end > > Outputs: > >> ["Work/Icon\r", "Work/cvs", "Work/git", "Work/mirrors", "Work/misc", >> "Work/p4", "Work/svn"] > > glob "*" outputs all "visible" files (non-dot files) but it can do a whole > lot more too. ri Dir.glob for more details. Omitting all hidden entries may not be what OP wants. This skips not only '.' and '..', but also (eg) '.git'. I find this way more robust: Dir.entries(whatever)-['.','..']