Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Ruby > How do I build a tree of directories?

Reply
Thread Tools

How do I build a tree of directories?

 
 
Ben Knight
Guest
Posts: n/a
 
      07-24-2007
Hello. I need to send XML back to a client app listing all my
directories and sub-directories on the server. The XML will look
something like this:

<folders>
<folder name="directory-name">
<folder name="directory name"
<folder name="directory name">
</folder>
</folder>
</folder>
</folders>

I'm doing this in Rails. I have a couple of questions:

1. In my controller, how do I use something like Find.find(path) to do
this effectively? For example, do I need to make recursive calls to a
method for each subdirectory encountered? Do I build an array of arrays
for the sub-directories? A Hash?

2. Once my array of arrays or hash or whatever is built, what's the best
way to output this in my view?

Thanks in advance.
--
Posted via http://www.ruby-forum.com/.

 
Reply With Quote
 
 
 
 
Skye Shaw!@#$
Guest
Posts: n/a
 
      07-25-2007
On Jul 24, 3:06 pm, Ben Knight <anilsc...@yahoo.com> wrote:
> Hello. I need to send XML back to a client app listing all my
> directories and sub-directories on the server. The XML will look
> something like this:
>
> <folders>
> <folder name="directory-name">
> <folder name="directory name"
> <folder name="directory name">
> </folder>
> </folder>
> </folder>
> </folders>
>
> I'm doing this in Rails. I have a couple of questions:


> 1. In my controller, how do I use something like Find.find(path) to do
> this effectively? For example, do I need to make recursive calls to a
> method for each subdirectory encountered?


You'd want to use one of the Dir methods; foreach(), glob()...

Entry = Struct.new(:dir,:children)

def recurse(path)

entry = Entry.new(path,[])

#no "." or ".." dirs
Dir["#{path}/*"].each do |e|
if File.directory?(e)
entry.children << recurse(e)
end
end

entry

end

> Do I build an array of arrays for the sub-directories? A Hash?


Depends. If you just need the directory name and its children, then an
array of arrays will work. If you need additional information, then
use a Hash. Maybe a Struct (for fun).

> 2. Once my array of arrays or hash or whatever is built, what's the best
> way to output this in my view?


XML Builder, left as an exercise.

If you do not have additional requirements for the directory data,
i.e. you just want to output it as XML, then I'd say write the XML
with XML Builder as you recurse. Especially if your directory
structure is big.

 
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
Re: How include a large array? Edward A. Falk C Programming 1 04-04-2013 08:07 PM
B+ Tree versus Ternary Search Tree Ramkumar Menon Java 2 08-16-2005 08:13 PM
SWsoft Acronis Disk Director Suite 9.0 Build 508, Acronis OS Selector 8.0 Build 917, Acronis Partition Expert 2003 Build 292, Acronis Power Utilities 2004 Build 502, F-SECURE.ANTI vIRUS.PROXY v1.10.17.WINALL, F-SECURE.ANTI vIRUS v5.50.10260 for CITRI vvcd Computer Support 0 09-25-2004 01:38 AM
B tree, B+ tree and B* tree Stub C Programming 3 11-12-2003 01:51 PM
Spanning Tree And Per Vlan Spanning Tree Amy L. Cisco 0 07-24-2003 10:01 PM



Advertisments
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57