Skip to content

Instantly share code, notes, and snippets.

@sgottlieb
Created October 29, 2012 22:08
Show Gist options
  • Save sgottlieb/3976865 to your computer and use it in GitHub Desktop.
Save sgottlieb/3976865 to your computer and use it in GitHub Desktop.
Word count with Ruby
def open_file(filename)
word_list = []
aFile = File.open(filename, "r+")
aFile.each do |line|
list = line.split(" ")
list.each do |word|
word_list << word
end
end
aFile.close
return word_list
end
def make_dictionary(list_words)
word_dict = Hash.new(0)
list_words.each do |word|
word_dict[word] += 1
end
return word_dict
end
def main(filename)
word_list = open_file(filename)
word_dict = make_dictionary(word_list)
return word_dict.sort
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment