Skip to content

Commit

Permalink
Merge 5077bd8 into 6d8bba3
Browse files Browse the repository at this point in the history
  • Loading branch information
gonzedge committed May 11, 2023
2 parents 6d8bba3 + 5077bd8 commit 6e9b476
Show file tree
Hide file tree
Showing 4 changed files with 21 additions and 3 deletions.
2 changes: 2 additions & 0 deletions lib/rambling/trie/enumerable.rb
Expand Up @@ -23,6 +23,8 @@ def each
yield word
end
end

self
end
end
end
Expand Down
6 changes: 5 additions & 1 deletion lib/rambling/trie/readers/plain_text.rb
Expand Up @@ -10,7 +10,11 @@ class PlainText
# from.
# @yield [String] Each line read from the file.
def each_word filepath
File.foreach(filepath) { |line| yield line.chomp! }
return enum_for :each_word unless block_given?

::File.foreach(filepath) { |line| yield line.chomp! }

self
end
end
end
Expand Down
8 changes: 6 additions & 2 deletions spec/lib/rambling/trie/enumerable_spec.rb
Expand Up @@ -11,8 +11,8 @@ module Trie
before { add_words node, words }

describe '#each' do
it 'returns an enumerator' do
expect(node.each).to be_a Enumerator
it 'returns an enumerator when no block is given' do
expect(node.each).to be_an Enumerator
end

it 'has the same word count as the trie' do
Expand All @@ -22,6 +22,10 @@ module Trie
it 'includes every word contained in the trie' do
node.each { |word| expect(words).to include word }
end

it 'returns the enumerable when a block is given' do
expect(node.each { |_| }).to eq node
end
end

describe '#size' do
Expand Down
8 changes: 8 additions & 0 deletions spec/lib/rambling/trie/readers/plain_text_spec.rb
Expand Up @@ -14,5 +14,13 @@
reader.each_word(filepath) { |word| yielded << word }
expect(yielded).to eq words
end

it 'returns an enumerator when no block is given' do
expect(reader.each_word filepath).to be_an Enumerator
end

it 'returns the enumerable when a block is given' do
expect(reader.each_word(filepath) { |_| }).to eq reader
end
end
end

0 comments on commit 6e9b476

Please sign in to comment.