Skip to content

Instantly share code, notes, and snippets.

@vanmichael
Created December 11, 2013 04:56
Show Gist options
  • Save vanmichael/7905277 to your computer and use it in GitHub Desktop.
Save vanmichael/7905277 to your computer and use it in GitHub Desktop.
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
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