# Recursively finding children

**URL:** <https://www.ruby-forum.com/t/recursively-finding-children/76176>\
**Category:** Rails\
**Created:** [November 16, 2006, 2:59pm UTC](https://www.ruby-forum.com/t/recursively-finding-children/76176 "2006-11-16T14:59:14Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![Richard\_Inham](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/a88e57/32.png) [@Richard\_Inham](https://www.ruby-forum.com/u/Richard_Inham)\
**Post date:** [November 16, 2006, 2:59pm UTC](https://www.ruby-forum.com/t/recursively-finding-children/76176/1 "2006-11-16T14:59:14Z")

</div>

I’m trying to get a flattened array of children (down to a certain  
depth) of a folder (which is using acts\_as\_tree).

def recursive\_find\_depth(depth)  
if self.children.length \> 0 and depth != 0  
self.children.each do |child|  
puts child.name  
child.recursive\_find\_depth(depth -= 1)  
end  
end  
end

At the moment, the function ‘puts’ the right output, i.e. all the  
children under a certain folder to a certain depth. However, I  
completely stuck about how to get the finished array of children out of  
this function (this shouldn’t be hierarchal, just a flattened array). If  
anyone’s wondering what I’m using this for - it’s a webdav system  
(PROPFIND). Thanks in advance

---

<div class="post-metadata">

**Author:** ![Richard\_Inham](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/a88e57/32.png) [@Richard\_Inham](https://www.ruby-forum.com/u/Richard_Inham)\
**Post date:** [November 16, 2006, 3:02pm UTC](https://www.ruby-forum.com/t/recursively-finding-children/76176/2 "2006-11-16T15:02:25Z")

</div>

Richard Inham wrote:

> I’m trying to get a flattened array of children (down to a certain  
> depth) of a folder (which is using acts\_as\_tree).
> 
> def recursive\_find\_depth(depth)  
> if self.children.length \> 0 and depth != 0  
> self.children.each do |child|  
> puts child.name  
> child.recursive\_find\_depth(depth -= 1)  
> end  
> end  
> end
> 
> At the moment, the function ‘puts’ the right output, i.e. all the  
> children under a certain folder to a certain depth. However, I  
> completely stuck about how to get the finished array of children out of  
> this function (this shouldn’t be hierarchal, just a flattened array). If  
> anyone’s wondering what I’m using this for - it’s a webdav system  
> (PROPFIND). Thanks in advance

Will the Ruby Array.flatten method do what you’re after?

[http://www.rubycentral.com/ref/ref\_c\_array.html#flatten](http://www.rubycentral.com/ref/ref_c_array.html#flatten)

c.

---

<div class="post-metadata">

**Author:** ![Richard\_Inham](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/a88e57/32.png) [@Richard\_Inham](https://www.ruby-forum.com/u/Richard_Inham)\
**Post date:** [November 16, 2006, 3:06pm UTC](https://www.ruby-forum.com/t/recursively-finding-children/76176/3 "2006-11-16T15:06:05Z")

</div>

Cayce B. wrote:

> Richard Inham wrote:
> 
> > I’m trying to get a flattened array of children (down to a certain  
> > depth) of a folder (which is using acts\_as\_tree).
> > 
> > def recursive\_find\_depth(depth)  
> > if self.children.length \> 0 and depth != 0  
> > self.children.each do |child|  
> > puts child.name  
> > child.recursive\_find\_depth(depth -= 1)  
> > end  
> > end  
> > end
> > 
> > At the moment, the function ‘puts’ the right output, i.e. all the  
> > children under a certain folder to a certain depth. However, I  
> > completely stuck about how to get the finished array of children out of  
> > this function (this shouldn’t be hierarchal, just a flattened array). If  
> > anyone’s wondering what I’m using this for - it’s a webdav system  
> > (PROPFIND). Thanks in advance
> 
> Will the Ruby Array.flatten method do what you’re after?
> 
> [http://www.rubycentral.com/ref/ref\_c\_array.html#flatten](http://www.rubycentral.com/ref/ref_c_array.html#flatten)
> 
> c.

def recursive\_find\_depth(depth)  
if self.children.length \> 0 and depth != 0  
child\_array ||= []  
self.children.each do |child|  
child\_array \<\< child  
puts child\_array.join(’ ,')  
child.recursive\_find\_depth(depth -= 1)  
end  
return child\_array  
end  
end

Nope. The function above returns the first folder, but not ‘child\_array’  
(although it ‘puts’ it fine).

---

<div class="post-metadata">

**Author:** ![Richard\_Inham](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/a88e57/32.png) [@Richard\_Inham](https://www.ruby-forum.com/u/Richard_Inham)\
**Post date:** [November 16, 2006, 3:33pm UTC](https://www.ruby-forum.com/t/recursively-finding-children/76176/4 "2006-11-16T15:33:57Z")

</div>

Hi –

On Thu, 16 Nov 2006, Richard Inham wrote:

> end  
> end
> 
> At the moment, the function ‘puts’ the right output, i.e. all the  
> children under a certain folder to a certain depth. However, I  
> completely stuck about how to get the finished array of children out of  
> this function (this shouldn’t be hierarchal, just a flattened array). If  
> anyone’s wondering what I’m using this for - it’s a webdav system  
> (PROPFIND). Thanks in advance

This is untested, but hopefully will either work or give you some  
ideas:

def recursive\_find\_depth(depth, list=[])  
unless depth.zero?  
children.each do |child|  
list.push(child.name)  
child.recursive\_find\_depth(depth-1, list)  
end  
end  
list  
end

David

–  
David A. Black | removed\_email\_address@domain.invalid  
Author of “Ruby for Rails” [1] | Ruby/Rails training & consultancy [3]  
DABlog (DAB’s Weblog) [2] | Co-director, Ruby Central, Inc. [4]  
[1] [Ruby for Rails](http://www.manning.com/black) | [3] [http://www.rubypowerandlight.com](http://www.rubypowerandlight.com)  
[2] [http://dablog.rubypal.com](http://dablog.rubypal.com) | [4] [http://www.rubycentral.org](http://www.rubycentral.org)

---

<div class="post-metadata">

**Author:** ![Richard\_Inham](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/a88e57/32.png) [@Richard\_Inham](https://www.ruby-forum.com/u/Richard_Inham)\
**Post date:** [November 16, 2006, 4:21pm UTC](https://www.ruby-forum.com/t/recursively-finding-children/76176/5 "2006-11-16T16:21:31Z")

</div>

> This is untested, but hopefully will either work or give you some  
> ideas:

Thanks David, works fine!

---

<div class="post-metadata">

**Author:** ![Richard\_Inham](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/a88e57/32.png) [@Richard\_Inham](https://www.ruby-forum.com/u/Richard_Inham)\
**Post date:** [November 16, 2006, 3:44pm UTC](https://www.ruby-forum.com/t/recursively-finding-children/76176/6 "2006-11-16T15:44:00Z")

</div>

On 16 November 2006 17:06, Richard Inham wrote:

> > > ```
> > > end
> > > 
> > > ```
> > 
> > Will the Ruby Array.flatten method do what you’re after?  
> > puts child\_array.join(’ ,’)  
> > child.recursive\_find\_depth(depth -= 1)  
> > end  
> > return child\_array  
> > end  
> > end
> 
> Nope. The function above returns the first folder, but not ‘child\_array’  
> (although it ‘puts’ it fine).

def nflatten(depth, result = [])  
return [] if depth \<= 0  
self.inject([]) { |result, item| result \<\< item \<\< nflatten(depth-1,  
item);  
result }  
end
