.sparkymat


Building an “include” graph

Posted on August 6th, 2006 ~ 08:08:38 PM
Tagged as | 1 Comment »

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
[/ruby]

Any comments?


File extensions in ruby

Posted on August 6th, 2006 ~ 12:06:47 AM
Tagged as | 1 Comment »

Is there a more efficient way to extract the file extensions in ruby? I currently do the following.

[ruby]

ext = File.basename(filepath).reverse.split(‘.’)[0].reverse

[/ruby]


Powered by WordPress | DOS_FX skin by Monzilla | All content copyright (c) 2006 sparkymat | 9 database queries served in 0.1924322 seconds