# Recurse through array of arrays

**URL:** <https://www.ruby-forum.com/t/recurse-through-array-of-arrays/191701>\
**Category:** Ruby\
**Created:** [July 16, 2010, 2:16pm UTC](https://www.ruby-forum.com/t/recurse-through-array-of-arrays/191701 "2010-07-16T14:16:36Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![tensaiji](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/6de8d8/32.png) [@tensaiji](https://www.ruby-forum.com/u/tensaiji)\
**Post date:** [July 16, 2010, 2:16pm UTC](https://www.ruby-forum.com/t/recurse-through-array-of-arrays/191701/1 "2010-07-16T14:16:36Z")

</div>

Hi,

I’ve written the method below to recurse through a non-binary tree of  
arbitrary depth. I’m still new to Ruby and was wondering if there were  
some Rubyism’s (or just general pointers) that might improve it?

Each object ‘t’ holds an array ‘children’ of objects of the same type as  
t, which can themselves hold further arrays. Each object also has the  
attribute ‘parent’ holding a reference to the parent object.

def find\_all( block )  
selves = []

```
#find_all'
f = lambda { |s|
  if block.call( s )
    selves.push( s )
  elsif s.children
    s.children.each { |c| f.call( c ) }
  else
    return
  end
}

f.call( self )

return selves

```

end

Then I can call it like this:

t.find\_all( lambda { |x| return x if x.whatever\_attribute == ‘anything’  
} )

It works, but any thoughts or input are much appreciated.

Regards  
Iain

---

<div class="post-metadata">

**Author:** ![tensaiji](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/6de8d8/32.png) [@tensaiji](https://www.ruby-forum.com/u/tensaiji)\
**Post date:** [July 16, 2010, 2:24pm UTC](https://www.ruby-forum.com/t/recurse-through-array-of-arrays/191701/2 "2010-07-16T14:24:12Z")

</div>

On 16 Jul 2010, at 13:07, Iain B. wrote:

> Each object ‘t’ …

Pardon me, ‘t’ is the instance of the object here, if that wasn’t clear.

Iain

---

<div class="post-metadata">

**Author:** ![tensaiji](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/6de8d8/32.png) [@tensaiji](https://www.ruby-forum.com/u/tensaiji)\
**Post date:** [July 16, 2010, 4:17pm UTC](https://www.ruby-forum.com/t/recurse-through-array-of-arrays/191701/3 "2010-07-16T16:17:36Z")

</div>

Iain B. wrote:

> On 16 Jul 2010, at 13:07, Iain B. wrote:
> 
> > Each object ‘t’ …
> 
> Pardon me, ‘t’ is the instance of the object here, if that wasn’t clear.
> 
> Iain

result = t.flatten.select { |e| e.whatever\_attr == “anything” }

---

<div class="post-metadata">

**Author:** ![tensaiji](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/6de8d8/32.png) [@tensaiji](https://www.ruby-forum.com/u/tensaiji)\
**Post date:** [July 16, 2010, 4:23pm UTC](https://www.ruby-forum.com/t/recurse-through-array-of-arrays/191701/4 "2010-07-16T16:23:17Z")

</div>

Jeff M. wrote:

> Iain B. wrote:
> 
> > On 16 Jul 2010, at 13:07, Iain B. wrote:
> > 
> > > Each object ‘t’ …
> > 
> > Pardon me, ‘t’ is the instance of the object here, if that wasn’t clear.
> > 
> > Iain
> 
> result = t.flatten.select { |e| e.whatever\_attr == “anything” }

oh yes…

should specify some max\_depth for flatten

max\_depth = 99  
result = t.flatten(max\_depth).select { |e| e.whatever\_attr == “anything”  
}

---

<div class="post-metadata">

**Author:** ![tensaiji](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/6de8d8/32.png) [@tensaiji](https://www.ruby-forum.com/u/tensaiji)\
**Post date:** [July 16, 2010, 9:46pm UTC](https://www.ruby-forum.com/t/recurse-through-array-of-arrays/191701/5 "2010-07-16T21:46:50Z")

</div>

On 16 Jul 2010, at 15:23, Jeff M. wrote:

> > > Iain  
> > > –  
> > > Posted via [http://www.ruby-forum.com/](http://www.ruby-forum.com/).

Thanks very much. Can’t argue with a 12 line into 2 line reduction! 🙂

Iain
