Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 25 additions & 0 deletions count.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
class Counter
def initialize
file = File.open("speech.txt", "r")
words = []

file.each do |line|
words.concat(line.split)
end

# Initialize a new hash with default key values of 0
word_count = Hash.new(0)

words.each do |word|
# Strip and replace anything that is not a-z
word = word.downcase.gsub(/[^a-z]/, '')
word_count[word] += 1
end

# Reverse our order, show the most frequently typed words first
word_count = word_count.sort_by{ |k, v| v }.reverse!
puts word_count
end
end

Counter.new
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It is unusual to do all the work in initialize; I would expect Counter.new to prepare a counter object ready to do the work.

It is a bit arbitrary which parts of the code are setup and which are the real work. Perhaps reading the file still belongs in initialize. I like to do some of the prep work in there.