Skip to content
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
7 changes: 2 additions & 5 deletions lib/circular_array.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,8 @@ class CircularArray < Array
VERSION = '0.1.0'

def [](index)
result = super
return nil if empty?

return result if result
return nil if size.zero?

self[index - size]
super(index % size)
end
end
8 changes: 8 additions & 0 deletions spec/circular_array_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,14 @@
expect(circular_array[17]).to eq :c
expect(circular_array[18]).to eq :a
end

it 'no recursion' do
# this will detect accidentally introduced recursion
allow(circular_array).to receive(:[]).and_call_original
expect(circular_array).to receive(:[]).exactly(2).times
expect(circular_array[1]).to eq :b
expect(circular_array[10]).to eq :b
end
end

context 'empty' do
Expand Down