Skip to content

Instantly share code, notes, and snippets.

@chastell
Created December 15, 2009 18:32
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save chastell/257164 to your computer and use it in GitHub Desktop.
Save chastell/257164 to your computer and use it in GitHub Desktop.
words = Hash.new 0
Dir.glob("#{ARGV.first}/**/*").reject { |file| File.directory? file }.each do |file|
File.read(file).scan(/\w+/).each { |word| words[word] += 1 }
end
File.open('counts-descreasing-ruby', 'w') do |file|
file << words.sort_by { |word, count| count }.reverse.map { |word, count| "#{word}\t#{count}" }.join("\n")
end
File.open('counts-alphabetical-ruby', 'w') do |file|
file << words.sort.map { |word, count| "#{word}\t#{count}" }.join("\n")
end
words = Dir.glob("#{ARGV.first}/**/*").reject { |file| File.directory? file }.map do |file|
File.read(file).scan(/\w+/)
end.flatten.inject(Hash.new(0)) { |hash, word| hash[word] += 1; hash }
File.open('counts-descreasing-ruby', 'w') do |file|
file << words.sort_by { |word, count| count }.reverse.map { |word, count| "#{word}\t#{count}" }.join("\n")
end
File.open('counts-alphabetical-ruby', 'w') do |file|
file << words.sort.map { |word, count| "#{word}\t#{count}" }.join("\n")
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment