mirror of
https://github.com/pry/pry.git
synced 2022-11-09 12:35:05 -05:00
ring: fix swap parts of array in to_a and index access
This commit is contained in:
parent
85850f47e0
commit
bb73fd1f19
2 changed files with 18 additions and 6 deletions
|
@ -57,10 +57,7 @@ class Pry
|
||||||
return @buffer[(count + index) % max_size] if index.is_a?(Integer)
|
return @buffer[(count + index) % max_size] if index.is_a?(Integer)
|
||||||
return @buffer[index] if count <= max_size
|
return @buffer[index] if count <= max_size
|
||||||
|
|
||||||
# Swap parts of array when the array turns page and starts overwriting
|
transpose_buffer_tail[index]
|
||||||
# from the beginning, then apply the range.
|
|
||||||
last_part = @buffer.slice([index.end, max_size - 1].min, count % max_size)
|
|
||||||
(last_part + (@buffer - last_part))[index]
|
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
@ -68,8 +65,7 @@ class Pry
|
||||||
def to_a
|
def to_a
|
||||||
return @buffer.dup if count <= max_size
|
return @buffer.dup if count <= max_size
|
||||||
|
|
||||||
last_part = @buffer.slice(count % max_size, @buffer.size)
|
transpose_buffer_tail
|
||||||
last_part + (@buffer - last_part)
|
|
||||||
end
|
end
|
||||||
|
|
||||||
# Clear the buffer and reset count.
|
# Clear the buffer and reset count.
|
||||||
|
@ -80,5 +76,12 @@ class Pry
|
||||||
@count = 0
|
@count = 0
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
private
|
||||||
|
|
||||||
|
def transpose_buffer_tail
|
||||||
|
tail = @buffer.slice(count % max_size, @buffer.size)
|
||||||
|
tail.concat @buffer.slice(0, count % max_size)
|
||||||
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
|
@ -13,6 +13,11 @@ describe Pry::Ring do
|
||||||
ring << 1 << 2 << 3 << 4 << 5
|
ring << 1 << 2 << 3 << 4 << 5
|
||||||
expect(ring.to_a).to eq([3, 4, 5])
|
expect(ring.to_a).to eq([3, 4, 5])
|
||||||
end
|
end
|
||||||
|
|
||||||
|
it "keeps duplicate elements" do
|
||||||
|
ring << 1 << 1 << 1 << 1
|
||||||
|
expect(ring.to_a).to eq([1, 1, 1])
|
||||||
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
describe "#[]" do
|
describe "#[]" do
|
||||||
|
@ -54,6 +59,10 @@ describe Pry::Ring do
|
||||||
expect(ring[-3]).to eq(3)
|
expect(ring[-3]).to eq(3)
|
||||||
end
|
end
|
||||||
|
|
||||||
|
it "returns the first element when accessed through 0..0" do
|
||||||
|
expect(ring[0..0]).to eq([3])
|
||||||
|
end
|
||||||
|
|
||||||
it "reads elements via inclusive range" do
|
it "reads elements via inclusive range" do
|
||||||
expect(ring[1..2]).to eq([4, 5])
|
expect(ring[1..2]).to eq([4, 5])
|
||||||
expect(ring[-2..-1]).to eq([4, 5])
|
expect(ring[-2..-1]).to eq([4, 5])
|
||||||
|
|
Loading…
Reference in a new issue