0
0
Fork 0

returns all lines, which have a match

This commit is contained in:
Gibheer 2011-05-10 14:36:37 +02:00
parent 99fa77b82b
commit 4274de4f02
3 changed files with 13 additions and 11 deletions

View File

@ -29,11 +29,12 @@ class Polecat
def search term
File.open @path + '/index.txt' do |f|
linenr = 0
matches = []
while (line = f.gets) do
return linenr if line =~ /#{term}/
matches << linenr if line =~ /#{term}/
linenr += 1
end
nil
matches
end
end
end

View File

@ -1 +0,0 @@
foo

View File

@ -62,17 +62,19 @@ describe "Index" do
@file = @path + '/index.txt'
end
it "returns the line of the first found occurence" do
i = Polecat::Index.new @path
i.write "foo"
i.search("foo").should == 0
end
it "returns nil, when no match was found" do
it "returns an array of lines, where the match was found" do
i = Polecat::Index.new @path
i.write "foo"
i.write "bar"
i.search("baz").should == nil
i.write "foo"
i.search("foo").should == [0, 2]
end
it "returns an empty array, when no match was found" do
i = Polecat::Index.new @path
i.write "foo"
i.write "bar"
i.search("baz").should == []
end
end
end