Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
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
You can’t perform that action at this time.