Kind of nested loops

V

Veedo Dream

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

}}
 
V

Veedo Dream

And this code works when I explicitly give the array name

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

}}
 
R

Ryan Davis

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 =3D {
"log" =3D> {
'where' =3D> 'blocks/where.rb',
'when' =3D> 'blocks/when.rb',
'bank' =3D> 'blocks/bank.rb',
},
"top" =3D> {
'where' =3D> 'blocks/where.rb',
'when' =3D> 'blocks/when.rb',
'bank' =3D> '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.
 
A

Alex Stahl

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.
 
V

Veedo Dream

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,
 
R

Rin Zebramädchen

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 =3D [ "log " ,
=C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0"top" =C2=A0]


log =3D [

=C2=A0 =C2=A0 { 'where' =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =
=C2=A0 =C2=A0 =C2=A0=3D> =C2=A0'/home/des/blocks/where.rb'},
=C2=A0 =C2=A0 { 'when' =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =
=C2=A0 =C2=A0 =C2=A0 =3D> =C2=A0'/home/des/blocks/when.rb'},
=C2=A0 =C2=A0 { 'bank' =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =
=C2=A0 =C2=A0 =C2=A0 =3D> =C2=A0'/home/des/blocks/bank.rb'}
]
top =3D [

=C2=A0 =C2=A0 { 'where' =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =
=C2=A0 =C2=A0 =C2=A0=3D> =C2=A0'/home/top/blocks/where.rb'},
=C2=A0 =C2=A0 { 'when' =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =
=C2=A0 =C2=A0 =C2=A0 =3D> =C2=A0'/home/top/blocks/when.rb'},
=C2=A0 =C2=A0 { 'bank' =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =
=C2=A0 =C2=A0 =C2=A0 =3D> =C2=A0'/home/top/blocks/bank.rb'}
]


build.each{ |bu|
=C2=A0 =C2=A0puts bu
=C2=A0 =C2=A0bu.each{ |files|
=C2=A0 =C2=A0 =C2=A0puts files

}}

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

log =3D [
{ :where =3D> '/home/des/blocks/where.rb'},
{ :when =3D> '/home/des/blocks/when.rb'},
{ :bank =3D> '/home/des/blocks/bank.rb'}
]

top =3D [

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

build =3D {:top =3D> top, :log =3D> 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
 
J

Jeremy Bopp

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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,780
Messages
2,569,611
Members
45,276
Latest member
Sawatmakal

Latest Threads

Top