Skip to content

Instantly share code, notes, and snippets.

@myronmarston
Created June 15, 2010 19:00
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save myronmarston/439525 to your computer and use it in GitHub Desktop.
Save myronmarston/439525 to your computer and use it in GitHub Desktop.
class DocumentAnalyzer
SPLIT_STRATEGY = lambda { |doc| doc.split(' ').size }
def initialize(document)
@document = document
end
def word_count(counting_strategy = SPLIT_STRATEGY)
counting_strategy.call(@document)
end
def phrase_count(phrase)
@document.scan(phrase).count
end
end
$LOAD_PATH.unshift(File.dirname(__FILE__))
$LOAD_PATH.unshift(File.join(File.dirname(__FILE__), '..', 'lib'))
require 'rubygems'
require 'spec'
require 'spec/autorun'
require 'document_analyzer'
begin
require 'ruby-debug'
Debugger.start
Debugger.settings[:autoeval] = true if Debugger.respond_to?(:settings)
rescue LoadError
# ruby-debug wasn't available so neither can the debugging be
end
Spec::Runner.configure do |config|
end
describe DocumentAnalyzer do
describe 'a new instance' do
subject { DocumentAnalyzer.new("this is a sentence with some words") }
it 'returns the word count from #word_count' do
subject.word_count.should == 7
end
end
it 'counts words for a document with double spaces and tabs correctly' do
da = DocumentAnalyzer.new("these\t is some words")
da.word_count.should == 4
end
it 'counts phrases' do
da = DocumentAnalyzer.new("this is a sentence with some words. this is a sentence with some words")
da.phrase_count('some words').should == 2
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment