# Hacking arrays

**URL:** <https://www.ruby-forum.com/t/hacking-arrays/136120>\
**Category:** Ruby\
**Created:** [April 29, 2008, 4:11am UTC](https://www.ruby-forum.com/t/hacking-arrays/136120 "2008-04-29T04:11:08Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![john\_old](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/9f8e36/32.png) [@john\_old](https://www.ruby-forum.com/u/john_old)\
**Post date:** [April 29, 2008, 4:11am UTC](https://www.ruby-forum.com/t/hacking-arrays/136120/1 "2008-04-29T04:11:08Z")

</div>

The code:

[http://pastie.caboo.se/188207](http://pastie.caboo.se/188207)

The question:

Why isn’t it doing them like nested folders? I’d like to get

.-----------------  
| Parent folder  
| .------------------  
| | child folder  
| | child files  
| |\_\_\_\_\_\_\_\_\_\_\_\_

* * *

but I don’t. Can anyone see whats wrong here? I’m losing my mind.  
Thanks.

---

<div class="post-metadata">

**Author:** ![john\_old](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/9f8e36/32.png) [@john\_old](https://www.ruby-forum.com/u/john_old)\
**Post date:** [April 29, 2008, 5:20am UTC](https://www.ruby-forum.com/t/hacking-arrays/136120/2 "2008-04-29T05:20:27Z")

</div>

Hi –

On Tue, 29 Apr 2008, John wrote:

> | .------------------  
> | | child folder  
> | | child files  
> | |\_\_\_\_\_\_\_\_\_\_\_\_
> 
> * * *
> 
> but I don’t. Can anyone see whats wrong here? I’m losing my mind.

It probably just a matter of counting the divs. I’ve sort of hacked  
together a rewrite that looks like it’s getting it right. Emphasis on  
“hacked” 🙂 There’s room for refinement, but anyway, this might get  
you moving. [http://pastie.org/188490](http://pastie.org/188490)

David

---

<div class="post-metadata">

**Author:** ![john\_old](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/9f8e36/32.png) [@john\_old](https://www.ruby-forum.com/u/john_old)\
**Post date:** [April 29, 2008, 9:17am UTC](https://www.ruby-forum.com/t/hacking-arrays/136120/3 "2008-04-29T09:17:36Z")

</div>

On Apr 28, 2008, at 8:10 PM, John wrote:

> | .------------------  
> | | child folder  
> | | child files  
> | |\_\_\_\_\_\_\_\_\_\_\_\_
> 
> * * *
> 
> but I don’t. Can anyone see whats wrong here? I’m losing my mind.  
> Thanks.

it looks like you are making it much harder than it needs to be…  
this code gives you the depth you are at all the way down without the  
need to count:

cfp:~ \> find a  
a  
a/b  
a/b/c  
a/b/c/file.rb  
a/b/file.rb  
a/file.rb

cfp:~ \> cat a.rb

def div folder = ‘.’, depth = 0  
title = folder

entries = Dir[“#{ folder }/\*”]

padding = ’ ’ \* depth

files = entries.select{|entry| test ?f, entry}  
dirs = entries.select{|entry| test ?d, entry}

dir\_content = dirs.map do |dir|  
div dir, depth + 1  
end

file\_content = files.map do |file|  
content = \<\<-html  
#{ file }  
html  
end

content = \<\<-html

  

 #{ title } 
  
#{ file\_content }  
#{ dir\_content }  

  
  
html

indent = content[%r/^\s\*/]  
content.strip.gsub %r/^#{ indent }/, padding  
end

puts div( ARGV.shift )

cfp:~ \> ruby a.rb a

 a 
a/file.rb

 a/b 
a/b/file.rb

```
<div class='folder'>
<div class='title'> a/b/c </div>
<span class='file'>a/b/c/file.rb</span>

</div>

```

the indentation could be improved… but i suppose that doesn’t matter  
for html anyhow.

regards.

a @ [http://codeforpeople.com/](http://codeforpeople.com/)

---

<div class="post-metadata">

**Author:** ![john\_old](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/9f8e36/32.png) [@john\_old](https://www.ruby-forum.com/u/john_old)\
**Post date:** [April 29, 2008, 9:48pm UTC](https://www.ruby-forum.com/t/hacking-arrays/136120/4 "2008-04-29T21:48:50Z")

</div>

The anwser to Phlips question is ‘I don’t know how to write unit  
tests.’

---

<div class="post-metadata">

**Author:** ![john\_old](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/9f8e36/32.png) [@john\_old](https://www.ruby-forum.com/u/john_old)\
**Post date:** [April 29, 2008, 9:24pm UTC](https://www.ruby-forum.com/t/hacking-arrays/136120/5 "2008-04-29T21:24:41Z")

</div>

> Where are the unit tests?
> 
> –  
> Phlip

That’s a good question, a fair question, and I would love to anwser it.

---

<div class="post-metadata">

**Author:** ![john\_old](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/9f8e36/32.png) [@john\_old](https://www.ruby-forum.com/u/john_old)\
**Post date:** [April 29, 2008, 10:04pm UTC](https://www.ruby-forum.com/t/hacking-arrays/136120/6 "2008-04-29T22:04:48Z")

</div>

-----BEGIN PGP SIGNED MESSAGE-----  
Hash: SHA1

John wrote:  
| The anwser to Phlips question is ‘I don’t know how to write unit  
| tests.’  
|  
|

And I guess you’d like to know how to do that?

Well, Ruby ships with Test::Unit  
[http://ruby-doc.org/core/classes/Test/Unit.html](http://ruby-doc.org/core/classes/Test/Unit.html)

Then, there’s Shoulda:  
[http://www.thoughtbot.com/projects/shoulda](http://www.thoughtbot.com/projects/shoulda)

RSpec:

> **[RSpec: Behaviour Driven Development for Ruby](http://rspec.info/)**

Just to name three options you have. 😉

Ruby on Rails books usually cover Unit Testing (though, Rails brings its  
own additions to the framework!).

And here’s a quick guide to unit testing in Ruby:

[http://ruby.about.com/od/learnruby/p/learn\_by\_test.htm?rd=1](http://ruby.about.com/od/learnruby/p/learn_by_test.htm?rd=1)

* * *

Phillip G.  
Twitter: [twitter.com/cynicalryan](http://twitter.com/cynicalryan)

Zoo: An excellent place to study the habits of human beings.  
~ – Evan Esar  
-----BEGIN PGP SIGNATURE-----  
Version: GnuPG v1.4.8 (MingW32)  
Comment: Using GnuPG with Mozilla - [http://enigmail.mozdev.org](http://enigmail.mozdev.org)

iEYEARECAAYFAkgWj4YACgkQbtAgaoJTgL+XGwCfaRgvqP+ALvYgYc8hB58ZMxBL  
28YAoI+/nR5jhs9OBcOf26v0uONYpRDL  
=GJO/  
-----END PGP SIGNATURE-----

---

<div class="post-metadata">

**Author:** ![john\_old](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/9f8e36/32.png) [@john\_old](https://www.ruby-forum.com/u/john_old)\
**Post date:** [April 29, 2008, 10:01pm UTC](https://www.ruby-forum.com/t/hacking-arrays/136120/7 "2008-04-29T22:01:15Z")

</div>

> It probably just a matter of counting the divs. I’ve sort of hacked  
> together a rewrite that looks like it’s getting it right. Emphasis on  
> “hacked” 🙂 There’s room for refinement, but anyway, this might get  
> you moving.[http://pastie.org/188490](http://pastie.org/188490)
> 
> David

That works, perfect. David, thanks. Looks like the num function was  
the problem. I couldn’t see it - the array intersection wasn’t even  
part of the equation.
