From: rio4ruby Date: 2007-06-09T12:05:05+09:00 Subject: Re: Newb Wants to Better His File, Dir code On Jun 8, 7:31 pm, R. B. Love wrote: > I've got code working to do what I want but I'm hoping there is a > better way. Mine seems clumsy. > > My goal is to search over all files, identify all that are directories > and then do something to each directory. I don't need to go into > directories recursively yet. > > The fact that Dir.foreach returns strings seems odd to me. All advice > appreciated. > > #!/usr/local/bin/ruby > > dpath = Dir.getwd > > Dir.foreach(dpath) do > |x| > ddir = File.join(dpath,x) > if File.directory?(ddir) && x != "." && x != ".." then > puts %x{du -sh #{x}} > end > end One way simplify your handling of files and directories is to use Rio. This will require you to install a module that is not a part of the standard Ruby distribution. The easiest way to do this is: gem install rio (or on Unix or Linux: sudo gem install rio) Then your code can be reduced to: require 'rio' rio.dirs do |x| puts %x{du -sh #{x}} end