Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Ruby > recursive output of a directory and its sub-directories

Reply
Thread Tools

recursive output of a directory and its sub-directories

 
 
Alexander Fleck
Guest
Posts: n/a
 
      07-06-2007
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.

 
Reply With Quote
 
 
 
 
come
Guest
Posts: n/a
 
      07-06-2007
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" <(E-Mail Removed)> 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.



 
Reply With Quote
 
 
 
 
Alexander Fleck
Guest
Posts: n/a
 
      07-06-2007
thanks,

I now use the 'find' module.

Alex.

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Its a bird, its a plane, its.. um, an Attribute based System? thunk Ruby 14 04-03-2010 10:08 AM
Its a bird, its a plane, its.. um, an Attribute based System? thunk Ruby 0 04-01-2010 10:25 PM
Its a bird, its a plane, no ummm, its a Ruide thunk Ruby 1 03-30-2010 11:10 AM
Recursive delete of directory - Directory not empty error Gordon Javascript 1 04-14-2008 06:28 PM
Two recursive calls inside of a recursive function n00m C++ 12 03-13-2008 03:18 PM



Advertisments