How easy would it be to build include graphs in ruby?
Here's my first attempt:
RUBY:
-
$list_of_files = Hash.new
-
$list_of_files.each {|node|
-
node = Array.new
-
}
-
def store_includes(pathname)
-
lines = IO.readlines(pathname)
-
lines.each {|line|
-
if line.include? "#include"
-
include_file = line[line.index("#include")+"#include".size,line.size]
-
fidx = 0
-
sidx = 0
-
-
if include_file.include?("\"")
-
fidx = include_file.index("\"") + 1
-
sidx = include_file.index("\"",fidx+1) - 1
-
end
-
-
if include_file.include?('<')
-
fidx = include_file.index('<') + 1
-
sidx = include_file.index('>') - 1
-
end
-
-
include_file = include_file[fidx..sidx]
-
$list_of_files[pathname].add(include_file)
-
end
-
}
-
end
-
-
def iterate_files(pathname)
-
list = [] #alternate way of initializing an array
-
if pathname=="." or pathname==nil
-
list = Dir["*"]
-
else
-
list = Dir[pathname + File::SEPERATOR + "*"]
-
end
-
list.each { |path|
-
if File.filetype(path) == "directory"
-
iterate_files(pathname)
-
end
-
if File.filetype(path) == "file"
-
store_includes(path)
-
end
-
}
-
end
Any comments?
shajith says:
If you use regexes, the code would probably look more concise. Regex might be overkill for this case, though. Also, use File.directory? and File.file? methods. The code could be ‘ruby-fied’ a lot too, I think - maybe I’ll give it a shot when things quiet down here.
Do post more Ruby
August 7, 2006, 7:53 am