# Listing file tree

**URL:** <https://www.ruby-forum.com/t/listing-file-tree/171259>\
**Category:** Rails\
**Created:** [June 29, 2009, 5:38pm UTC](https://www.ruby-forum.com/t/listing-file-tree/171259 "2009-06-29T17:38:06Z")\
**Posts on this page:** 1\
**Page:** 1

<div class="post-metadata">

**Author:** ![christoffer](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/e56c9b/32.png) [@christoffer](https://www.ruby-forum.com/u/christoffer)\
**Post date:** [June 29, 2009, 5:38pm UTC](https://www.ruby-forum.com/t/listing-file-tree/171259/1 "2009-06-29T17:38:06Z")

</div>

Hey there,

I am programming a file tree listing for my rails project. I found some  
code that seemed to do a nice job that would be perfect after some  
styling.

This works smoothly in pure ruby when run on the console. However it  
does not produce the results I want when integrated with Rails.

This is the model that is producing the result.

require ‘pathname’

class Filetree \< ActiveRecord::Base

$result = []

def self.return\_filetree\_for\_project(id)

```
$ArmMap = Hash.new("| ")
$ArmMap[""] = ""

$ArmMap["`"] = " "

ARGV << "." if ARGV.empty?

ARGV.map{ |path| visit(Pathname.new("/"), "", "", "",

```

Pathname.new(path) ) }

```
return $result

```

end

private

```
def self.visit(path, leader, tie, arm, node)

  $result << "#{leader}#{arm}#{tie}#{node}<br/>"

  visit_children(path + node, leader + $ArmMap[arm])

end

def self.visit_children(path, leader)

  return unless FileTest.directory? path
  return unless FileTest.readable? path

  files = path.children(false).sort #false = return name, not

```

full path

```
  return if files.empty?

  arms = Array.new(files.length - 1, "|") << "`"

  pairs = files.zip(arms)
  pairs.each { |e| visit(path, leader, "-- ", e[1], e[0]) }

end

```

end

And the output:

webrick  
–port  
3000

I have tried using various mixtures of the path, but I can only manage  
to produce either this or an empty result. Do you have any ideas?

Thank you in advance,  
Christoffer
