From: bbense+comp.lang.ruby.Mar.18.02@... Date: 2002-03-19T01:24:28+09:00 Subject: Re: Some potential RCRs -----BEGIN PGP SIGNED MESSAGE----- In article <20020316062744.59D021131@sharui.nakada.kanuma.tochigi.jp>, wrote: >Hi, > >At Sat, 16 Mar 2002 06:16:40 +0900, >; wrote: >> > >> >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? > ># Array doesn't have class method `slice'. - - Sorry, I'm still figuring out when to use .,::,#. > >Dir includes Enumerable not but extend it. So you can use >Dir#grep, Dir#collect and so on. > >> 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. > >It looks like Dir.entries except for the test, but the name >`slice' may cause confusion? It's not close to Array#slice. > - - Yes, slice is probably a bad name. In fact I think if ri Dir listed the methods available via Enumerable we wouldn't be having this debate. It's pretty clear to me after some messing around that using Dir.entries is almost always the wrong yet obvious thing to do. After I figured out that Dir uses enumerable it seems to me a much more "Ruby-like" way to do things is to use code like this. curDir = Dir.new(".") files = curDir.find_all { |a| FileTest.file?(a) } or cfiles = curDir.grep(/\.c$/) { |a| FileTest.file?(a) } - - It's clearer code and fundementally more secure. After all class methods should be the exception, not the rule. - - Booker C. Bense -----BEGIN PGP SIGNATURE----- Version: 2.6.2 iQCVAwUBPJYRbAD83u1ILnWNAQHP/QP+I4AgGGeNW05SLz0D9+a5TaR7JFoCzw0P UDa6jGjRRIDsmTVF76JuyD2iaMSx5Th3PIKSLVGUab7MMHq5HPiurvS+YzqB/Fdt m7ZgK2O9GQGUJj/o+iIR3a6WJVbFJIbeEmVI4Dj7gM3eM+kWg8BIt8tbhzttr0Yi txJXFX9LpD4= =LPp5 -----END PGP SIGNATURE----- --