[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.ruby

recursive output of a directory and its sub-directories

Alexander Fleck

7/6/2007 6:45:00 AM

hi,
I want to walk through a directory recursively.

I have the follwing code:

'
#!/usr/bin/env ruby

def search
Dir.foreach(".\\") { |x|
if File.directory?(x)
search
else
open(x) do |file|
file.each { |l| puts l}
end
puts x
end
}
end

search
'

I get a 'stack level too deep'-error. What does that mean and how can I
avoid it?

thanks,
Alex.

2 Answers

come

7/6/2007 7:06:00 AM

0

Hi,

You reach this limit because you are stacking forever: your search
method has no argument, so no recursion occures. You have to pass the
path of the subdirectory to search into to the search method.

Another (better) way is to use the "find" standard library of Ruby.

require "find"

Find.find(".") do |f|
puts f
end

(I haven't tested this code but it should work).
It will do the work for you.

Regards,
Come

On 6 juil, 08:45, "Alexander Fleck" <F...@schleissheimer.de> wrote:
> hi,
> I want to walk through a directory recursively.
>
> I have the follwing code:
>
> '
> #!/usr/bin/env ruby
>
> def search
> Dir.foreach(".\\") { |x|
> if File.directory?(x)
> search
> else
> open(x) do |file|
> file.each { |l| puts l}
> end
> puts x
> end
> }
> end
>
> search
> '
>
> I get a 'stack level too deep'-error. What does that mean and how can I
> avoid it?
>
> thanks,
> Alex.


Alexander Fleck

7/6/2007 8:53:00 AM

0

thanks,

I now use the 'find' module.

Alex.