From: bbense+comp.lang.ruby.Mar.15.02@... Date: 2002-03-16T06:16:40+09:00 Subject: Re: Some potential RCRs -----BEGIN PGP SIGNED MESSAGE----- In article <99DD35F8BFC4D511A3C500508BEE03A23246C0@mail01>, Morris, Chris wrote: >> > - - A much better solution would be added support for file "type" >> > matching. I.e. give me all the "simple" files in this directory >> > or give me all the symlinks, or all the subdirs... Something >> > similar to the kind of knobs that unix find gives you. >> >> Exactly what I was getting at. > >Something like ... > > def Dir.files(dirname) > result = [] > Dir.entries(dirname).each do |entry| > result << entry if File.stat(dirname + entry).file? > end > result > end > >As Guy posted, Dir[dirname + '*'] will do the same, except it includes the >directory name in each entry. > >Maybe these sort of functions would be good additions to ftools.rb? > - - I did some more thinking about this and it seems to me that a good name would be Dir::slice. Somewhat similar in concept to Array::slice. Or perhaps Dir::collect. Hmm, does Dir implement Emunerable? Here's a simple minded implementation. # This implements the method # Dir::slice(dirname,test, proc ) # It returns an array of names that pass the test and # if proc is available runs that on them and returns # that resulting array. class Dir def Dir.slice (dirname,test) result = [] Dir.entries(dirname).each do |entry| if test.call(dirname + "/" + entry) then if ( block_given? ) then result << yield(dirname + "/" + entry) else result << entry end end end result end def Dir.files (dirname) Dir.slice(dirname,Proc.new{ |a| FileTest.file?(a) }) end end puts Dir.files( ".") puts Dir.slice(".", Proc.new{ |a| FileTest.file?(a) }) { |a| a.upcase } - - Booker C. Bense -----BEGIN PGP SIGNATURE----- Version: 2.6.2 iQCVAwUBPJJfbwD83u1ILnWNAQFDvwP/QoAphkFS76hYJilRsArWTYjEmBUwaok/ eYSMTkRDdS5qopzmFzs3IucT4EldotccQu+jw5aRiVqr/EjEWQLsaxEGMsWznN2g 50un4dR8xq87bSerQu5zLyC5theyHUbCLw9nxiazdjWx8u2pY+F7kOuC9SwZLDcf GzjjJpBJA4A= =9+d1 -----END PGP SIGNATURE----- --