# Kind of nested loops

**URL:** <https://www.ruby-forum.com/t/kind-of-nested-loops/197474>\
**Category:** Ruby\
**Created:** [October 14, 2010, 11:30pm UTC](https://www.ruby-forum.com/t/kind-of-nested-loops/197474 "2010-10-14T23:30:05Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![Dobai-Pataky\_BSSSSl](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/77aa72/32.png) [@Dobai-Pataky\_BSSSSl](https://www.ruby-forum.com/u/Dobai-Pataky_BSSSSl)\
**Post date:** [October 14, 2010, 11:30pm UTC](https://www.ruby-forum.com/t/kind-of-nested-loops/197474/1 "2010-10-14T23:30:05Z")

</div>

I am trying to get this going where i have this build array and then I  
have an array of hashes named after the items in the build. Depending  
upon what is in the array, I want to read the arrays of hashes. I wrote  
this simple script, but does not work. Any solns  
build = [ "log " ,  
“top” ]

log = [

```
 { 'where' => '/home/des/blocks/where.rb'},
 { 'when' => '/home/des/blocks/when.rb'},
 { 'bank' => '/home/des/blocks/bank.rb'}

```

]  
top = [

```
 { 'where' => '/home/top/blocks/where.rb'},
 { 'when' => '/home/top/blocks/when.rb'},
 { 'bank' => '/home/top/blocks/bank.rb'}

```

]

build.each{ |bu|  
puts bu  
bu.each{ |files|  
puts files

}}

---

<div class="post-metadata">

**Author:** ![casper2008](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/bcef8e/32.png) [@casper2008](https://www.ruby-forum.com/u/casper2008)\
**Post date:** [October 14, 2010, 11:34pm UTC](https://www.ruby-forum.com/t/kind-of-nested-loops/197474/2 "2010-10-14T23:34:40Z")

</div>

And this code works when I explicitly give the array name

build.each{ |bu|  
puts bu  
top.each{ |files|  
puts files

}}

---

<div class="post-metadata">

**Author:** ![casper2008](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/bcef8e/32.png) [@casper2008](https://www.ruby-forum.com/u/casper2008)\
**Post date:** [October 14, 2010, 11:51pm UTC](https://www.ruby-forum.com/t/kind-of-nested-loops/197474/3 "2010-10-14T23:51:23Z")

</div>

On Oct 14, 2010, at 14:30 , Veedo D. wrote:

> I am trying to get this going where i have this build array and then I  
> have an array of hashes named after the items in the build. Depending  
> upon what is in the array, I want to read the arrays of hashes. I wrote  
> this simple script, but does not work. Any solns

Meta: I’d first suggest that you not build your own build system and  
look at rake instead.

NonMeta: Instead of splitting everything up and nesting your loops, nest  
your data structure instead:

build = {  
“log” =\> {  
‘where’ =\> ‘blocks/where.rb’,  
‘when’ =\> ‘blocks/when.rb’,  
‘bank’ =\> ‘blocks/bank.rb’,  
},  
“top” =\> {  
‘where’ =\> ‘blocks/where.rb’,  
‘when’ =\> ‘blocks/when.rb’,  
‘bank’ =\> ‘blocks/bank.rb’,  
}  
}

I didn’t see any point in having an array of hashes with single pairs in  
them, so I took that out too.

Absolute paths in build systems are brittle, use relative paths instead.

---

<div class="post-metadata">

**Author:** ![casper2008](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/bcef8e/32.png) [@casper2008](https://www.ruby-forum.com/u/casper2008)\
**Post date:** [October 14, 2010, 11:55pm UTC](https://www.ruby-forum.com/t/kind-of-nested-loops/197474/4 "2010-10-14T23:55:46Z")

</div>

Er, why would you want to store an array of hashes like that, instead of  
just a hash? It would make iterating much easier to use a hash like so:

log = {  
‘where’ =\> ‘/path/to/fileA’,  
‘when’ =\> ‘/path/to/fileB’,  
‘bank’ =\> ‘/path/to/fileN’  
}

#repeat w/ ‘top’

build = [log, top]

Then, “build[0][‘where’] = ‘/path/to/fileA’”.

Note that, in your implementation, the first array only contains the  
strings "log " and “top”, and not the hashes, so my last line would  
evaluate to nil, not the path.

---

<div class="post-metadata">

**Author:** ![casper2008](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/bcef8e/32.png) [@casper2008](https://www.ruby-forum.com/u/casper2008)\
**Post date:** [October 15, 2010, 12:28am UTC](https://www.ruby-forum.com/t/kind-of-nested-loops/197474/5 "2010-10-15T00:28:21Z")

</div>

On 10/14/2010 4:56 PM, Veedo D. wrote:

> A good idea of using rake, but I have a reason not to use rake and build  
> my own system. Users here should be able to comment off what they don’t  
> want to built. I know it is crude, but that is the way we want to go.  
> The reason I have the files located in a different array as I have a lot  
> many files than 3 to comment off.

I can’t add anything more than has already been said for the direct  
question; however, I felt a word of warning is in order.

At the company where I work, we have invented a number of build tools to  
help developers streamline their build process and improve build  
reliability. As the current maintainer of these tools, I can speak  
personally to the fact that unless your organization is in the business  
of selling build tools or strongly dedicated the effort for other  
reasons, you will most likely saddle those users with overly  
complicated, unsupported tools that no one really knows how to use.

That is exactly what has happened for us. Virtually nothing is spent to  
properly support these tools or provide decent training and  
documentation for their use. As a result, our work is more often  
hampered by the tools than helped these days, but it is unbelievably  
difficult to claw your way out after you’re in, no matter how painful  
the home grown tool is to support and use. It’s difficult to convince  
managers to delay new features and bug fixes so that your team has time  
to refactor their code and daily work activities for a new build tool.

I have spoken with other people in the same line of work, and it’s the  
same story just about every time. If you really need something special,  
try to extend an existing tool with as little custom code as possible.  
Rake can be quite flexible if you’re clever. After all, it’s just Ruby  
in the end. Writing your own build tool from scratch is almost always a  
bad idea.

Just my 2 cents.

-Jeremy

---

<div class="post-metadata">

**Author:** ![casper2008](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/bcef8e/32.png) [@casper2008](https://www.ruby-forum.com/u/casper2008)\
**Post date:** [October 14, 2010, 11:56pm UTC](https://www.ruby-forum.com/t/kind-of-nested-loops/197474/6 "2010-10-14T23:56:26Z")

</div>

A good idea of using rake, but I have a reason not to use rake and build  
my own system. Users here should be able to comment off what they don’t  
want to built. I know it is crude, but that is the way we want to go.  
The reason I have the files located in a different array as I have a lot  
many files than 3 to comment off.

Thanks,

---

<div class="post-metadata">

**Author:** ![casper2008](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/bcef8e/32.png) [@casper2008](https://www.ruby-forum.com/u/casper2008)\
**Post date:** [October 15, 2010, 10:59pm UTC](https://www.ruby-forum.com/t/kind-of-nested-loops/197474/7 "2010-10-15T22:59:10Z")

</div>

On Thu, Oct 14, 2010 at 11:30 PM, Veedo D. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> ```
> { 'where' => '/home/des/blocks/where.rb'},
> 
> ```

Maybe I’m missing the point (not quite sure if I got what you want to  
do),  
but how about this?

log = [  
{ :where =\> ‘/home/des/blocks/where.rb’},  
{ :when =\> ‘/home/des/blocks/when.rb’},  
{ :bank =\> ‘/home/des/blocks/bank.rb’}  
]

top = [

```
{ :where => '/home/top/blocks/where.rb'},
{ :when => '/home/top/blocks/when.rb'},
{ :bank => '/home/top/blocks/bank.rb'}

```

]

build = {:top =\> top, :log =\> log}

build.each do |bu|  
bu.each do |files|  
puts files  
end  
end

* * *

log  
where/home/des/blocks/where.rb  
when/home/des/blocks/when.rb  
bank/home/des/blocks/bank.rb  
top  
where/home/top/blocks/where.rb  
when/home/top/blocks/when.rb  
bank/home/top/blocks/bank.rb
