File tree Expand file tree Collapse file tree 2 files changed +8
-7
lines changed Expand file tree Collapse file tree 2 files changed +8
-7
lines changed Original file line number Diff line number Diff line change @@ -4,11 +4,7 @@ class CircularArray < Array
44 VERSION = '0.1.0'
55
66 def []( index )
7- result = super
8-
9- return result if result
10- return nil if size . zero?
11-
12- self [ index - size ]
7+ return nil if empty?
8+ super ( index % size )
139 end
1410end
Original file line number Diff line number Diff line change 1111
1212 it 'behaives like array' do
1313 expect ( circular_array ) . to be_kind_of Array
14-
14+
15+ # this will detect accidentally introduced recursion
16+ allow ( circular_array ) . to receive ( :[] ) . and_call_original
17+ expect ( circular_array ) . to receive ( :[] ) . exactly ( 4 ) . times
18+
1519 expect ( circular_array [ 0 ] ) . to eq :a
1620 expect ( circular_array [ 1 ] ) . to eq :b
1721 expect ( circular_array [ 2 ] ) . to eq :c
22+ expect ( circular_array [ 10 ] ) . to eq :b
1823 end
1924
2025 it 'endless' do
You can’t perform that action at this time.
0 commit comments