Hello --

On Sat, 24 Feb 2001, Neil Conway wrote:

> Hi all,
> 
> I'm still a Ruby-newbie, so if this is silly, just tell me.

That's "Nuby" -- save yourself some typing :-)

> def process_file(file, array)
>     return unless File.exists?(file)
>     return unless File.readable?(file)
>     if (File.directory?(file))
>         return unless $RECURSIVE
>         dir = Dir.new(file)
>         dir.each {|f| process_file(f, array) }
>     end
>     array.push(file)
> end
> 
> ($RECURSIVE is a boolean global variable).
> 
> The problem with this is that Dir#each includes "." (and "..").

Have a look at Dir.glob, compared with Dir.new:

irb 1> Dir.new(".").entries
   ==>[".", "..", "somefile", "anotherfile", "thirdfile"]
irb 2> Dir.glob("*")
   ==>["somefile", "anotherfile", "thirdfile"]


Example of how you might use this (together with some
implicit suggestions about the code logic :-)

   def process_file(file,ary=[])
     if File.directory?(file) && $RECURSIVE
       Dir.glob("#{file}/*") do |f|
	 process_file(f,ary)
       end
     elsif File.readable?(file)
       ary.push file
     end
     return ary
   end

   files = process_file("name")


(and there may be other/better ways involving perhaps the
Find module)


David

-- 
David Alan Black
home: dblack / candle.superlink.net
work: blackdav / shu.edu
Web:  http://pirate.shu.edu/~blackdav