Skip to content

Instantly share code, notes, and snippets.

@vikas95prasad
Created January 20, 2019 18:56
Show Gist options
  • Save vikas95prasad/7606b1830841d46dedd5fc5e7e928f54 to your computer and use it in GitHub Desktop.
Save vikas95prasad/7606b1830841d46dedd5fc5e7e928f54 to your computer and use it in GitHub Desktop.
Get word count of a file in ruby
file = File.open("SampleTextFile_100kb.txt", "r")
words_print = {}
file.each_line do |line|
words = line.split
words.each do |word|
puts "previous word => #{word}"
word = word.downcase.gsub(/[,()'".]+\z/,'')
puts "afterwardsword"
if words_print[word]
words_print[word] += 1
else
words_print[word] = 1
end
end
end
puts words_print
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment