------art_29002_31158997.1192643239481
Content-Type: text/plain; charset=ISO-8859-1
Content-Transfer-Encoding: 7bit
Content-Disposition: inline

You can get a hold of the block as an argument:

def scan(&block)
  ...
      dinf.scan(&block)
  ...

  block.call(@path, @files)
end

Jason

On 10/17/07, Luis <lcrespom / gmail.com> wrote:
>
> Hello,
>
> I am trying to code a method that accepts a block... easy. The problem
> is that this method is recursive, so I don't know how to pass the
> block through the recursive calls.
>
> The specific code is about recurising through a directory tree while
> calling the provided code block once per each directory, passing the
> list of files in that directory. This is the code:
>
> class DirInfo
>
>         # constructor, etc. removed for clarity
>
>         def scan
>                 @path.each_entry do |p|
>                         next if p.to_s "." || p.to_s ".."
>                         p  path + p
>                         if p.file?
>                                 finf  ileInfo.new(p)
>                                 @files << finf
>                         elsif p.directory?
>                                 dinf  irInfo.new(p)
>                                 dinf.scan       #  Recursive search of
> subdirs
>                         end
>                 end
>                 yield @path, @files
>         end
>
> end
>
>
> So if I do:
>
> di  irInfo.new("/devtools/ruby")
> di.scan do |path, files|
>         puts "Directory #{path} has #{files.length} files"
> end
>
> I expect to get my block called once per each subdirectory, passing
> the subdir path and list of files in that subdir. The problem is that
> the fisrt time the call scan is made from within scan, no block is
> given, so I get a "no block given" exception and the program stops.
>
> I know there are other ways to scan directories, but please take it
> just as an example. Still the question is, how to combine
> recursiveness with blocks?
>
> Thanks in advance,
>
>    Luis.
>
>
>

------art_29002_31158997.1192643239481--