Skip to content

Conversation

@ritchedt
Copy link

@ritchedt ritchedt commented Nov 5, 2016

No description provided.

@@ -0,0 +1,43 @@
class CountWords

attr_accessor :word_count
Copy link
Member

Choose a reason for hiding this comment

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

This word_count isn't used yet. You can probably delete all the @@s and then you would be using it.

count_file = File.open(file, "r")
count_file.each_line do |line|
next if line.empty?
@@word_count = Hash.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 seems like we start with a fresh Hash on every line. Is that a bug?

word = line.split
word.each do |wrd|
next if wrd !~ /(\w+)/
wrd = wrd[/(\w+)/]
Copy link
Member

Choose a reason for hiding this comment

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

Splitting the line, iterating, and making sure each word matches \w+, can be simplified using String#scan.

@@word_count[wrd] = 1
end
end
display_word_count_asc
Copy link
Member

Choose a reason for hiding this comment

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

I think it would be better to not display while you build up the data. How about if the client code (outside this class) asks for the display something like:

word_counter = CountWords.new
word_counter.count_words_from_file("speech.txt")
word_counter.display_word_count_asc

def display_word_count_asc
puts ""
puts "'''''"
@@word_count.sort{|a,b| a[1]<=>b[1]}.each { |word|
Copy link
Member

Choose a reason for hiding this comment

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

Rubyists tend to use {} for single line block, and do/end for multiline blocks.

Check out Rubocop for automated checks for stuff like this. It enforces this style guide.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants