[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.ruby

hacking arrays

John

4/29/2008 2:09:00 AM

The code:

http://pastie.caboo...

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

Phlip

4/29/2008 2:18:00 AM

0

John wrote:

> http://pastie.caboo...

Where are the unit tests?

--
Phlip


John

4/29/2008 2:20:00 AM

0


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

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

John

4/29/2008 2:42:00 AM

0

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

Phillip Gawlowski

4/29/2008 3:01:00 AM

0

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

Then, there's Shoulda:
http://www.thoughtbot.com/projec...

RSpec:
http://r...

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

- --
Phillip Gawlowski
Twitter: 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....

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

David A. Black

4/29/2008 3:20:00 AM

0

Hi --

On Tue, 29 Apr 2008, John wrote:

> The code:
>
> http://pastie.caboo...
>
> 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.

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


David

--
Rails training from David A. Black and Ruby Power and Light:
INTRO TO RAILS June 9-12 Berlin
ADVANCING WITH RAILS June 16-19 Berlin
INTRO TO RAILS June 24-27 London (Skills Matter)
See http://www.r... for details and updates!

ara.t.howard

4/29/2008 7:17:00 AM

0


On Apr 28, 2008, at 8:10 PM, John wrote:
> The code:
>
> http://pastie.caboo...
>
> 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.



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
<span class='file'>#{ file }</span>
html
end

content = <<-html
<div class='folder'>
<div class='title'> #{ title } </div>
#{ file_content }
#{ dir_content }
</div>
</div>
html

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


puts div( ARGV.shift )


cfp:~ > ruby a.rb a
<div class='folder'>
<div class='title'> a </div>
<span class='file'>a/file.rb</span>

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

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


</div>
</div>
</div>
</div>
</div>
</div>




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

regards.




a @ http://codeforp...
--
we can deny everything, except that we have the possibility of being
better. simply reflect on that.
h.h. the 14th dalai lama




John

4/29/2008 7:56:00 PM

0

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