diff --git a/lib/pry/code.rb b/lib/pry/code.rb index ffab6246..4232742b 100644 --- a/lib/pry/code.rb +++ b/lib/pry/code.rb @@ -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 diff --git a/test/test_code.rb b/test/test_code.rb index ad14fbdd..8c7a7eb0 100644 --- a/test/test_code.rb +++ b/test/test_code.rb @@ -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