Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Ensure #each/#each_word return Enumerator/self #37

Merged
merged 1 commit into from
May 11, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions lib/rambling/trie/enumerable.rb
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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