Created
December 11, 2013 04:56
-
-
Save vanmichael/7905277 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
require 'pry' | |
class WordTracker | |
attr_reader :words | |
def initialize(words) | |
@words = words | |
@frequency = Hash.new(0) | |
end | |
def frequency | |
collection.each do |word| | |
@frequency[word.downcase] += 1 | |
end | |
@frequency | |
end | |
def collection | |
@words.split(' ') | |
end | |
end | |
WordTracker.new('Toy boat toy boat toy boat').frequency |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
require 'rspec' | |
require_relative 'word_tracker' | |
describe WordTracker do | |
it 'calculates the frequency of each word' do | |
expect(WordTracker.new('Toy boat toy boat toy boat').frequency).to eql ({ | |
"toy" => 3, | |
"boat" => 3 | |
}) | |
end | |
it 'should should seperate the words' do | |
expect(WordTracker.new('Toy boat toy boat toy boat').collection).to eql (['Toy','boat','toy','boat','toy','boat']) | |
end | |
it 'should have words' do | |
expect(WordTracker.new('Toy boat toy boat toy boat').words).to eql ('Toy boat toy boat toy boat') | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment