Skip to content

Instantly share code, notes, and snippets.

Created July 6, 2013 11:48

Revisions

  1. @invalid-email-address Anonymous created this gist Jul 6, 2013.
    11 changes: 11 additions & 0 deletions gistfile1.rb
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,11 @@
    puts 'What is the name and path of the file?'

    filename = gets.chomp
    text = String.new
    File.open(filename) { |f| text = f.read }
    words = text.split(/[^a-zA-Z]/)
    freqs = Hash.new(0)
    words.each { |word| freqs[word] += 1 }
    freqs = freqs.sort_by {|x,y| y }
    freqs.reverse!
    freqs.each {|word, freq| puts word+' '+freq.to_s}