diff --git a/lib/irb/ext/eval_history.rb b/lib/irb/ext/eval_history.rb index 6c21ff00e..dd1173382 100644 --- a/lib/irb/ext/eval_history.rb +++ b/lib/irb/ext/eval_history.rb @@ -97,8 +97,8 @@ def initialize(size = 16) # :nodoc: end def size(size) # :nodoc: - if size != 0 && size < @size - @contents = @contents[@size - size .. @size] + if size.positive? && @contents.size > size + @contents = @contents.last(size) end @size = size end diff --git a/test/irb/test_eval_history.rb b/test/irb/test_eval_history.rb index 685ad679a..b1d480636 100644 --- a/test/irb/test_eval_history.rb +++ b/test/irb/test_eval_history.rb @@ -49,5 +49,16 @@ def test_eval_history_respects_given_limit # Because eval_history injects `__` into the history AND decide to ignore it, we only get - 1 results assert_match("2 \"bar\"\n" + "3 \"baz\"\n" + "4 \"xyz\"\n", out) end + + def test_eval_history_can_shrink_before_reaching_configured_limit + out, err = execute_lines( + "IRB.CurrentContext.eval_history = 2", + "__", + conf: { EVAL_HISTORY: 5 } + ) + + assert_empty(err) + assert_match("=> 2\n" + "=> 1 2\n", out) + end end end