Created
December 11, 2013 05:47
-
-
Save sdanko11/7905627 to your computer and use it in GitHub Desktop.
word counter
This file contains hidden or 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' | |
| class WordCounter | |
| def initialize(phrase) | |
| @phrase = phrase | |
| @all_words = [] | |
| @uniq_words = [] | |
| @word_count = [] | |
| end | |
| def split_words | |
| @all_words = @phrase.split( ) | |
| get_uniq_words | |
| end | |
| def get_uniq_words | |
| @uniq_words = @all_words.uniq | |
| @uniq_words.each do |word| | |
| words_hash = {word =>0} | |
| @word_count << words_hash | |
| end | |
| @word_count | |
| count | |
| end | |
| def count | |
| @word_count.each do |count| | |
| @all_words.each do |each_word| | |
| if count.has_key?(each_word) | |
| count[each_word] +=1 | |
| end | |
| end | |
| end | |
| @word_count | |
| end | |
| end | |
| word =WordCounter.new("this is is is an array") | |
| word.split_words |
This file contains hidden or 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' | |
| describe WordCounter do | |
| let(:phrase) { WordCounter.new("this is a new phrase") } | |
| let(:phrase1) { WordCounter.new("this this is is") } | |
| let(:phrase2) { WordCounter.new("hi hi hi hi hi is is") } | |
| it 'all the words in the phrase' do | |
| expect(phrase.split_words).to eql([{'this'=> 1}, {'is'=> 1}, {'a' => 1}, {'new' => 1},{'phrase'=> 1}]) | |
| end | |
| it 'should count all the words in the phrase' do | |
| expect(phrase1.split_words).to eql([{'this'=> 2}, {'is'=> 2}]) | |
| end | |
| it 'it should count all the words in the phrase' do | |
| expect(phrase2.split_words).to eql([{'hi' => 5}, {'is' => 2}]) | |
| end | |
| end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment