Skip to content

Instantly share code, notes, and snippets.

@kitallis
Created August 14, 2009 18:32
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save kitallis/168003 to your computer and use it in GitHub Desktop.
Save kitallis/168003 to your computer and use it in GitHub Desktop.
# Creates a little histogram of the frequency of words occuring in your string
# Usage : ruby word_histogram.rb "Test string, string"
a=(ARGV[0].split(/[^\w]/).inject(Hash.new(0)){|h,x|h[x.downcase]+=1;h;}.delete_if{|k,v|k==""}.sort_by{|k,v| k.length}.reverse);a.map{|k,v|puts "=> #{k} ".ljust(a.first[0].size+4)+'*'*v+"\n"}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment