recursive output of a directory and its sub-directories

  • Thread starter Alexander Fleck
  • Start date
A

Alexander Fleck

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.
 
C

come

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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,995
Messages
2,570,228
Members
46,818
Latest member
SapanaCarpetStudio

Latest Threads

Top