# Parse a tree

**URL:** <https://www.ruby-forum.com/t/parse-a-tree/161943>\
**Category:** Ruby\
**Created:** [March 4, 2009, 3:55am UTC](https://www.ruby-forum.com/t/parse-a-tree/161943 "2009-03-04T03:55:57Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![Haris\_BogdanoviS\_S](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/h/a8b319/32.png) [@Haris\_BogdanoviS\_S](https://www.ruby-forum.com/u/Haris_BogdanoviS_S)\
**Post date:** [March 4, 2009, 3:55am UTC](https://www.ruby-forum.com/t/parse-a-tree/161943/1 "2009-03-04T03:55:57Z")

</div>

Hi.

How can I parse a tree (arrays within arrays), for example a tree of  
strings, to print all strings ?

Thanks.

---

<div class="post-metadata">

**Author:** ![Haris\_BogdanoviS\_S](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/h/a8b319/32.png) [@Haris\_BogdanoviS\_S](https://www.ruby-forum.com/u/Haris_BogdanoviS_S)\
**Post date:** [March 4, 2009, 4:19am UTC](https://www.ruby-forum.com/t/parse-a-tree/161943/2 "2009-03-04T04:19:21Z")

</div>

Haris wrote:

> Hi.
> 
> How can I parse a tree (arrays within arrays), for example a tree of  
> strings, to print all strings ?
> 
> Thanks.

tree walking hints at a recursive routine…

def tree\_walk(array)  
array.each do |item|  
if item.class == Array  
tree\_walk(item)  
else  
puts item  
end  
end  
end

or for the one-liners among us  
def tree\_walk(array)  
array.each { |item| (item.class == Array)? tree\_walk(item) : puts  
item }  
end

However, I don’t reccomend testing on the item.class. I can’t explain  
why very well, but it seems wrong to me. Maybe someone else can…?

# ======================================================================= This email, including any attachments, is only for the intended addressee. It is subject to copyright, is confidential and may be the subject of legal or other privilege, none of which is waived or lost by reason of this transmission. If the receiver is not the intended addressee, please accept our apologies, notify us by return, delete all copies and perform no other act on the email. Unfortunately, we cannot warrant that the email has not been altered or corrupted during transmission.

---

<div class="post-metadata">

**Author:** ![Haris\_BogdanoviS\_S](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/h/a8b319/32.png) [@Haris\_BogdanoviS\_S](https://www.ruby-forum.com/u/Haris_BogdanoviS_S)\
**Post date:** [March 4, 2009, 10:08am UTC](https://www.ruby-forum.com/t/parse-a-tree/161943/3 "2009-03-04T10:08:58Z")

</div>

Michael M. wrote:

> However, I don’t reccomend testing on the item.class. I can’t explain  
> why very well, but it seems wrong to me. Maybe someone else can…?

I think it’s fine. You need some way to distinguish between a leaf node  
and an inner node, and how you do that depends on how your data  
structure is built.

Here, the assumption is that inner nodes are Arrays and leaf nodes are  
anything else.

---

<div class="post-metadata">

**Author:** ![Haris\_BogdanoviS\_S](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/h/a8b319/32.png) [@Haris\_BogdanoviS\_S](https://www.ruby-forum.com/u/Haris_BogdanoviS_S)\
**Post date:** [March 4, 2009, 4:39am UTC](https://www.ruby-forum.com/t/parse-a-tree/161943/4 "2009-03-04T04:39:47Z")

</div>

From: Michael M. [[mailto:removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)]  
#…

# def tree\_walk(array)

# array.each { |item| (item.class == Array)?

```
                       ^^^^^^^^^^^^^^^^^^^^
                       item.is_a? Array

```

# tree\_walk(item) : puts item }

# end

note that the above tree\_walk is equivalent to,

puts array

---

<div class="post-metadata">

**Author:** ![Haris\_BogdanoviS\_S](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/h/a8b319/32.png) [@Haris\_BogdanoviS\_S](https://www.ruby-forum.com/u/Haris_BogdanoviS_S)\
**Post date:** [March 4, 2009, 12:20pm UTC](https://www.ruby-forum.com/t/parse-a-tree/161943/5 "2009-03-04T12:20:45Z")

</div>

On 04.03.2009 03:51, Haris Bogdanoviæ wrote:

> How can I parse a tree (arrays within arrays), for example a tree of  
> strings, to print all strings ?

Do you want to _parse_ or _print_? This is totally unclear to me. For  
displaying nested structures for debugging purposes, pp will do

require ‘pp’

a = […] # nested  
pp a

If you want to parse: you did not provide how the input looks like so  
nobody could help you with that.

Kind regards

robert
