class CodeStatistics

def calculate_directory_statistics(directory, pattern = /.*\.rb$/)

def calculate_directory_statistics(directory, pattern = /.*\.rb$/)
  stats = { "lines" => 0, "codelines" => 0, "classes" => 0, "methods" => 0 }
  Dir.foreach(directory) do |file_name|
    if File.stat(directory + "/" + file_name).directory? and (/^\./ !~ file_name)
      newstats = calculate_directory_statistics(directory + "/" + file_name, pattern)
      stats.each { |k, v| stats[k] += newstats[k] }
    end
    next unless file_name =~ pattern
    f = File.open(directory + "/" + file_name)
    while line = f.gets
      stats["lines"]     += 1
      stats["classes"]   += 1 if line =~ /class [A-Z]/
      stats["methods"]   += 1 if line =~ /def [a-z]/
      stats["codelines"] += 1 unless line =~ /^\s*$/ || line =~ /^\s*#/
    end
  end
  stats
end