How easy would it be to build include graphs in ruby?

Here's my first attempt:

RUBY:
  1. $list_of_files = Hash.new
  2. $list_of_files.each {|node|
  3. node = Array.new
  4. }
  5. def store_includes(pathname)
  6. lines = IO.readlines(pathname)
  7. lines.each {|line|
  8. if line.include? "#include"
  9. include_file = line[line.index("#include")+"#include".size,line.size]
  10. fidx = 0
  11. sidx = 0
  12.  
  13. if include_file.include?("\"")
  14. fidx = include_file.index("\"") + 1
  15. sidx = include_file.index("\"",fidx+1) - 1
  16. end
  17.  
  18. if include_file.include?('<')
  19. fidx = include_file.index('<') + 1
  20. sidx = include_file.index('>') - 1
  21. end
  22.  
  23. include_file = include_file[fidx..sidx]
  24. $list_of_files[pathname].add(include_file)
  25. end
  26. }
  27. end
  28.  
  29. def iterate_files(pathname)
  30. list = [] #alternate way of initializing an array
  31. if pathname=="." or pathname==nil
  32. list = Dir["*"]
  33. else
  34. list = Dir[pathname + File::SEPERATOR + "*"]
  35. end
  36. list.each { |path|
  37. if File.filetype(path) == "directory"
  38. iterate_files(pathname)
  39. end
  40. if File.filetype(path) == "file"
  41. store_includes(path)
  42. end
  43. }
  44. end

Any comments?

One Comment

  1. 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 :)

Leave a Reply