Skip to content

Commit

Permalink
Add Container#push to mirror Array#push interface (#60)
Browse files Browse the repository at this point in the history
And update `README.md` to include `#push` in docs.

Gotta thank rubocop's `Style/ConcatArrayLiterals` rule for the idea here!
  • Loading branch information
gonzedge committed Feb 20, 2024
1 parent b1b9278 commit 7a5e408
Show file tree
Hide file tree
Showing 3 changed files with 52 additions and 17 deletions.
8 changes: 7 additions & 1 deletion README.md
Expand Up @@ -90,10 +90,16 @@ trie.add 'word'
trie << 'word'
```

Or if you have multiple words to add, you can use `#concat`:
Or if you have multiple words to add, you can use `#concat` or `#push`:

``` ruby
trie.concat %w(a collection of words)
trie.push 'a', 'collection', 'of', 'words'

# or
words = %w(a collection of words)
trie.concat words
trie.push *words
```

And to find out if a word already exists in the trie, use `#word?` or its alias `#include?`:
Expand Down
11 changes: 11 additions & 0 deletions lib/rambling/trie/container.rb
Expand Up @@ -68,6 +68,17 @@ def partial_word? word = ''
root.partial_word? word.chars
end

# Adds all provided words to the trie.
# @param [Array<String>] words the words to add the branch from.
# @return [Array<Nodes::Node>] the collection of nodes added.
# @raise [InvalidOperation] if the trie is already compressed.
# @see #concat
# @see Nodes::Raw#add
# @see Nodes::Compressed#add
def push *words
concat words
end

# Checks if a whole word exists in the trie.
# @param [String] word the word to look for in the trie.
# @return [Boolean] +true+ only if the word is found and the last character corresponds to a terminal node,
Expand Down
50 changes: 34 additions & 16 deletions spec/lib/rambling/trie/container_spec.rb
Expand Up @@ -169,64 +169,82 @@
end
end

describe '#word?' do
it_behaves_like 'a propagating node' do
let(:method_name) { :word? }
describe '#push' do
# rubocop:disable RSpec/MultipleExpectations
it 'adds all the words to the root node' do
container.push 'other', 'words'

expect(root.children.size).to eq 2
expect(root.to_a).to eq %w(other words)
end

it 'returns all the corresponding nodes' do
nodes = container.push 'other', 'words'

expect(nodes.first.letter).to eq :o
expect(nodes.last.letter).to eq :w
end
# rubocop:enable RSpec/MultipleExpectations
end

describe '#partial_word?' do
context 'with underlying node' do
it_behaves_like 'a propagating node' do
let(:method_name) { :partial_word? }
end
end

context 'when word is contained' do
before { add_words container, %w(hello high) }

it_behaves_like 'a matching container#word'
it_behaves_like 'a matching container#partial_word'

context 'with compressed root' do
before { container.compress! }

it_behaves_like 'a matching container#word'
it_behaves_like 'a matching container#partial_word'
end
end

context 'when word is not contained' do
before { add_word container, 'hello' }

it_behaves_like 'a non-matching container#word'
it_behaves_like 'a non-matching container#partial_word'

context 'with compressed root' do
before { container.compress! }

it_behaves_like 'a non-matching container#word'
it_behaves_like 'a non-matching container#partial_word'
end
end
end

describe '#partial_word?' do
context 'with underlying node' do
it_behaves_like 'a propagating node' do
let(:method_name) { :partial_word? }
end
describe '#word?' do
it_behaves_like 'a propagating node' do
let(:method_name) { :word? }
end

context 'when word is contained' do
before { add_words container, %w(hello high) }

it_behaves_like 'a matching container#partial_word'
it_behaves_like 'a matching container#word'

context 'with compressed root' do
before { container.compress! }

it_behaves_like 'a matching container#partial_word'
it_behaves_like 'a matching container#word'
end
end

context 'when word is not contained' do
before { add_word container, 'hello' }

it_behaves_like 'a non-matching container#partial_word'
it_behaves_like 'a non-matching container#word'

context 'with compressed root' do
before { container.compress! }

it_behaves_like 'a non-matching container#partial_word'
it_behaves_like 'a non-matching container#word'
end
end
end
Expand Down

0 comments on commit 7a5e408

Please sign in to comment.