From 43283b2f374d7c9eeb35f89dca1a2949e231176f Mon Sep 17 00:00:00 2001 From: tomoya ishida Date: Sat, 15 Apr 2023 18:31:56 +0900 Subject: [PATCH] Change Reline.add_dialog_proc(name, nil) to properly remove dialog_proc (#532) --- lib/reline.rb | 8 ++++++-- test/reline/test_reline.rb | 3 +++ 2 files changed, 9 insertions(+), 2 deletions(-) diff --git a/lib/reline.rb b/lib/reline.rb index 210f33478f..a128d7347d 100644 --- a/lib/reline.rb +++ b/lib/reline.rb @@ -166,9 +166,13 @@ def dig_perfect_match_proc=(p) DialogProc = Struct.new(:dialog_proc, :context) def add_dialog_proc(name_sym, p, context = nil) - raise ArgumentError unless p.respond_to?(:call) or p.nil? raise ArgumentError unless name_sym.instance_of?(Symbol) - @dialog_proc_list[name_sym] = DialogProc.new(p, context) + if p.nil? + @dialog_proc_list.delete(name_sym) + else + raise ArgumentError unless p.respond_to?(:call) + @dialog_proc_list[name_sym] = DialogProc.new(p, context) + end end def dialog_proc(name_sym) diff --git a/test/reline/test_reline.rb b/test/reline/test_reline.rb index 9692605a00..460fb7872d 100644 --- a/test/reline/test_reline.rb +++ b/test/reline/test_reline.rb @@ -321,6 +321,9 @@ def test_add_dialog_proc d = Reline.dialog_proc(:test_proc) assert_equal(dummy_proc_2, d.dialog_proc) + Reline.add_dialog_proc(:test_proc, nil) + assert_nil(Reline.dialog_proc(:test_proc)) + l = lambda {} Reline.add_dialog_proc(:test_lambda, l) d = Reline.dialog_proc(:test_lambda)