1
0
Fork 0
mirror of https://github.com/pry/pry.git synced 2022-11-09 12:35:05 -05:00

fix line/index confusion in Code#between

This commit is contained in:
Ryan Fitzgerald 2012-01-15 14:16:18 -08:00
parent b177aa9aa1
commit 8879dfc6de
2 changed files with 21 additions and 5 deletions

View file

@ -138,7 +138,8 @@ class Pry
end
# Remove all lines that aren't in the given range, expressed either as a
# `Range` object or a first and last line number (inclusive).
# `Range` object or a first and last line number (inclusive). Negative
# indices count from the end of the array of lines.
#
# @param [Range, Fixnum] start_line
# @param [Fixnum?] end_line
@ -155,12 +156,20 @@ class Pry
end_line ||= start_line
end
start_line -= 1 unless start_line < 1
end_line -= 1 unless end_line < 1
range = start_line..end_line
if start_line > 0
start_idx = @lines.index { |l| l.last >= start_line } || @lines.length
else
start_idx = start_line
end
if end_line > 0
end_idx = (@lines.index { |l| l.last > end_line } || 0) - 1
else
end_idx = end_line
end
alter do
@lines = @lines[range] || []
@lines = @lines[start_idx..end_idx] || []
end
end

View file

@ -92,6 +92,13 @@ describe Pry::Code do
@code.should =~ /\A def self/
@code.should =~ /world!'\Z/
end
should 'use real line numbers for positive indices' do
@code = @code.after(3, 3)
@code = @code.between(4, 4)
@code.length.should == 1
@code.should =~ /\A end\Z/
end
end
describe '#before' do