returns all lines, which have a match
This commit is contained in:
parent
99fa77b82b
commit
4274de4f02
|
@ -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
|
||||
|
|
|
@ -1 +0,0 @@
|
|||
foo
|
|
@ -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
|
||||
|
|
Loading…
Reference in New Issue