# Newbie: need suggestion for "the ruby way"

**URL:** <https://www.ruby-forum.com/t/newbie-need-suggestion-for-the-ruby-way/71125>\
**Category:** Ruby\
**Created:** [February 21, 2006, 1:41am UTC](https://www.ruby-forum.com/t/newbie-need-suggestion-for-the-ruby-way/71125 "2006-02-21T01:41:59Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![kiol](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/k/ee59a6/32.png) [@kiol](https://www.ruby-forum.com/u/kiol)\
**Post date:** [February 21, 2006, 1:41am UTC](https://www.ruby-forum.com/t/newbie-need-suggestion-for-the-ruby-way/71125/1 "2006-02-21T01:41:59Z")

</div>

I write some code for generate file index for a directory.like this:  
#!/usr/local/bin/ruby

def parse\_options(args)  
require ‘optparse’  
require ‘ostruct’

options = OpenStruct.new  
opts = OptionParser.new(“Usage: genindex [options] paths”)  
options.dirs = []  
opts.on\_tail("-h", “–help”, “Show this message”) do  
puts opts  
exit  
end  
opts.on\_tail("-v", “–version”, “Show version”) do  
require ‘…/version’  
puts Version.join(’.’)  
exit  
end  
rest = opts.parse(args)  
if rest.empty?:  
puts opts  
else  
rest.each {|dir| options.dirs.push(Dir.new(File.expand\_path(dir)))}  
end  
if $DEBUG  
puts “get params:”  
options.dirs.each {|dir| puts dir.path}  
puts “end”  
end  
options  
end

def get\_indexs(curdir, indexs)  
puts ‘indexing ‘, curdir.path if $DEBUG  
curdir.each do |f|  
next if [’.svn’, ‘.’, ‘…’].include?(f)  
if File.directory?(File.join(curdir.path,f))  
get\_indexs(Dir.new(File.join(curdir.path,f)), indexs)  
else  
#it’s case insensitive  
indexs[f.downcase] ||= []  
indexs[f.downcase] += [File.join(curdir.path, f)]  
end  
end  
end

def create\_idx\_file(options)  
data\_dir = File.join(File.dirname(File.expand\_path( **FILE** )), ‘…’,  
‘data’)  
Dir.mkdir(data\_dir) if not File.exist?(data\_dir)  
Dir.chdir(data\_dir) do  
options.dirs.each do |dir|  
d = dir.path.split(File::SEPARATOR).join(’_’)  
next if Dir.foreach(’.’) do |idxdir|  
next if [’.’, ‘…’, ‘.svn’].include?(idxdir)  
 #sub dir need be ingored but same dir need recreate  
if d != idxdir and d.index(idxdir) == 0  
puts "path #{File.join(d.split(’_’))} is ingored."  
puts “Because it’s ancestors path  
#{File.join(idxdir.split(’\_’))} already been indexed.”  
break true  
end  
end

```
  Dir.mkdir(d) if not File.exist?(d)
  Dir.chdir(d) do
    File.open('indexs.yaml', 'w') do |idx_file|
      require 'yaml'
      indexs = {}
      get_indexs(dir, indexs)
      YAML.dump(indexs, idx_file)
    end
  end
end

```

end  
end

if $0 == **FILE**  
begin  
create\_idx\_file(parse\_options(ARGV))  
rescue Exception =\> e  
puts e.to\_s  
end  
end  
and the test code is :  
#!/usr/local/bin/ruby  
$:.unshift(File.join(File.dirname( **FILE** ),"…", “lib”))

require ‘test/unit’  
require ‘genindex’  
require ‘optparse’

class TextGenIndex \< Test::Unit::TestCase  
def test\_option\_parse  
args = ["."]  
assert\_equal(File.expand\_path("."),  
parse\_options(args).dirs[0].path)  
args = [’…’]  
assert\_equal(File.expand\_path("…"),  
parse\_options(args).dirs[0].path)  
args = ["-sdf"]  
assert\_raise(OptionParser::InvalidOption) {parse\_options(args)}  
args = ["/var","/tmp"]  
assert\_equal("/var", parse\_options(args).dirs[0].path)  
assert\_equal("/tmp", parse\_options(args).dirs[1].path)  
end  
def test\_create\_idx\_file  
data\_dir = File.join(File.dirname(File.expand\_path( **FILE** )),  
‘…’, ‘data’)  
args = [’.’]  
create\_idx\_file(parse\_options(args))  
assert\_equal(File.exist?(data\_dir), true)  
index\_dir = File.join(File.dirname(File.expand\_path( **FILE** )),  
‘…’, ‘data’, File.expand\_path(’.’).split(File::SEPARATOR).join(’_’))  
assert\_equal(File.exist?(index\_dir), true)  
args = [‘lib’, ‘test’]  
create\_idx\_file(parse\_options(args))  
assert\_equal(File.exist?(File.join(data\_dir,  
data\_dir.split(File::SEPARATOR)[0…-4].push(‘lib’).join(’_’))), false)  
require ‘yaml’  
index\_file = File.join(index\_dir, ‘indexs.yaml’)  
indexs = YAML.load(File.open(index\_file))  
assert\_equal(indexs[‘version.rb’.downcase][0],  
File.join(data\_dir.split(File::SEPARATOR)[0…-4], ‘version.rb’))  
end  
end

I need some help for going to “the ruby way”.  
this my first question on the group and I must apologize for my poor  
english and thanks a lot for your suggestion for my ruby code or my  
english.  
So, any suggestion is wellcome.

---

<div class="post-metadata">

**Author:** ![kiol](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/k/ee59a6/32.png) [@kiol](https://www.ruby-forum.com/u/kiol)\
**Post date:** [February 21, 2006, 7:09pm UTC](https://www.ruby-forum.com/t/newbie-need-suggestion-for-the-ruby-way/71125/2 "2006-02-21T19:09:49Z")

</div>

It might be more helpful to show what kind of output you want, rather  
than what code you’ve created.

However, for a simple recursive directory listing, look more into the  
Dir object:

depth = 1 #current or one directory down  
files = Dir[’\*\*/\*’].reject {|item|  
#filter out directories and directory depth  
File.directory?(item) or File.dirname(item).split(’/’).size \> depth  
}  
puts files

You can add more logic to filter out other files, organize them in a  
tree-like structure, etc. But, the Dir object and methods are VERY  
useful when used with File and other common Array and String methods.

---

<div class="post-metadata">

**Author:** ![kiol](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/k/ee59a6/32.png) [@kiol](https://www.ruby-forum.com/u/kiol)\
**Post date:** [February 22, 2006, 6:15am UTC](https://www.ruby-forum.com/t/newbie-need-suggestion-for-the-ruby-way/71125/3 "2006-02-22T06:15:19Z")

</div>

thanks a lot.  
I just want to index the files and only files in some directory.  
like this:  
dir\_a/dir\_b/file\_c  
dir\_a/file\_d  
will indexed as  
file\_c-\>dir\_a/dir\_b/file\_c  
file\_d-\>dir\_a/file\_d  
and dump this with YAML.

Now I think I can do that like this:  
Dir.chdir(d) do  
File.open(‘indexs.yaml’, ‘w’) do |idx\_file|  
require ‘yaml’  
indexs = {}  
Dir.chdir(dir.path) do  
Dir[’\*\*/\*’].reject {|item| File.directory?(item)}  
end  
YAML.dump(indexs, idx\_file)  
end  
end

so I do’t need the recursive procudure.

---

<div class="post-metadata">

**Author:** ![kiol](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/k/ee59a6/32.png) [@kiol](https://www.ruby-forum.com/u/kiol)\
**Post date:** [February 23, 2006, 1:35am UTC](https://www.ruby-forum.com/t/newbie-need-suggestion-for-the-ruby-way/71125/4 "2006-02-23T01:35:03Z")

</div>

yes, this is what i want.  
thanks very much.

---

<div class="post-metadata">

**Author:** ![kiol](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/k/ee59a6/32.png) [@kiol](https://www.ruby-forum.com/u/kiol)\
**Post date:** [February 22, 2006, 11:24am UTC](https://www.ruby-forum.com/t/newbie-need-suggestion-for-the-ruby-way/71125/5 "2006-02-22T11:24:01Z")

</div>

2006/2/21, Joby B. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid):

> }  
> puts files
> 
> You can add more logic to filter out other files, organize them in a  
> tree-like structure, etc. But, the Dir object and methods are VERY  
> useful when used with File and other common Array and String methods.

Not that there is module Find which has recursion built in:

require ‘find’  
Find.find(‘.’) {|f| puts f}

Kind regards

robert
