Skip to content

Commit 824473e

Browse files
committed
Make history infinite if set SAVE_HISTORY to negative
1 parent 5044eb2 commit 824473e

File tree

2 files changed

+25
-2
lines changed

2 files changed

+25
-2
lines changed

lib/irb/ext/save-history.rb

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -89,7 +89,7 @@ def load_history
8989
def save_history
9090
return unless self.class.const_defined?(:HISTORY)
9191
history = self.class::HISTORY
92-
if num = IRB.conf[:SAVE_HISTORY] and (num = num.to_i) > 0
92+
if num = IRB.conf[:SAVE_HISTORY] and (num = num.to_i) != 0
9393
if history_file = IRB.conf[:HISTORY_FILE]
9494
history_file = File.expand_path(history_file)
9595
end
@@ -110,7 +110,7 @@ def save_history
110110
open(history_file, "w:#{IRB.conf[:LC_MESSAGES].encoding}", 0600) do |f|
111111
hist = history.map{ |l| l.split("\n").join("\\\n") }
112112
begin
113-
hist = hist.last(num) if hist.size > num
113+
hist = hist.last(num) if hist.size > num and num > 0
114114
rescue RangeError # bignum too big to convert into `long'
115115
# Do nothing because the bignum should be treated as inifinity
116116
end

test/irb/test_history.rb

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,29 @@ def test_history_save_bignum
7575
HISTORY_FILE
7676
end
7777

78+
def test_history_save_minus_as_infinity
79+
result_output, result_history_file = launch_irb_with_irbrc_and_irb_history(<<~IRBRC, <<~IRB_HISTORY) do |stdin|
80+
IRB.conf[:USE_READLINE] = true
81+
IRB.conf[:SAVE_HISTORY] = -1 # infinity
82+
IRBRC
83+
1
84+
2
85+
3
86+
4
87+
IRB_HISTORY
88+
stdin.write("5\nexit\n")
89+
end
90+
91+
assert_equal(<<~HISTORY_FILE, result_history_file)
92+
1
93+
2
94+
3
95+
4
96+
5
97+
exit
98+
HISTORY_FILE
99+
end
100+
78101
private
79102

80103
def launch_irb_with_irbrc_and_irb_history(irbrc, irb_history)

0 commit comments

Comments
 (0)