Skip to content

Commit

Permalink
Fix exit! command warning and method behavior (#868)
Browse files Browse the repository at this point in the history
* Fix exit! command warning and method behavior

* Remove arg(0) from Kernel.exit and Kernel.exit!
  • Loading branch information
tompng authored Feb 12, 2024
1 parent 899d10a commit 372bc59
Show file tree
Hide file tree
Showing 5 changed files with 18 additions and 8 deletions.
2 changes: 1 addition & 1 deletion lib/irb.rb
Original file line number Diff line number Diff line change
Expand Up @@ -989,7 +989,7 @@ def run(conf = IRB.conf)
conf[:AT_EXIT].each{|hook| hook.call}

context.io.save_history if save_history
Kernel.exit(0) if forced_exit
Kernel.exit if forced_exit
end
end

Expand Down
2 changes: 1 addition & 1 deletion lib/irb/cmd/force_exit.rb
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ class ForceExit < Nop
def execute(*)
throw :IRB_EXIT, true
rescue UncaughtThrowError
Kernel.exit(0)
Kernel.exit!
end
end
end
Expand Down
6 changes: 3 additions & 3 deletions lib/irb/workspace.rb
Original file line number Diff line number Diff line change
Expand Up @@ -90,11 +90,11 @@ def initialize(*main)
IRB.conf[:__MAIN__] = @main
@main.singleton_class.class_eval do
private
define_method(:exit) do |*a, &b|
# Do nothing, will be overridden
end
define_method(:binding, Kernel.instance_method(:binding))
define_method(:local_variables, Kernel.instance_method(:local_variables))
# Define empty method to avoid delegator warning, will be overridden.
define_method(:exit) {|*a, &b| }
define_method(:exit!) {|*a, &b| }
end
@binding = eval("IRB.conf[:__MAIN__].instance_eval('binding', __FILE__, __LINE__)", @binding, *@binding.source_location)
end
Expand Down
12 changes: 12 additions & 0 deletions test/irb/cmd/test_force_exit.rb
Original file line number Diff line number Diff line change
Expand Up @@ -47,5 +47,17 @@ def foo

assert_match(/irb\(main\):001> 123/, output)
end

def test_forced_exit_out_of_irb_session
write_ruby <<~'ruby'
at_exit { puts 'un' + 'reachable' }
binding.irb
exit! # this will call exit! method overrided by command
ruby
output = run_ruby_file do
type "exit"
end
assert_not_include(output, 'unreachable')
end
end
end
4 changes: 1 addition & 3 deletions test/irb/test_cmd.rb
Original file line number Diff line number Diff line change
Expand Up @@ -58,9 +58,7 @@ def test_calling_command_on_a_frozen_main
"irb_info",
main: main
)
# Because the main object is frozen, IRB would wrap a delegator around it
# Which's exit! method can't be overridden and would raise a warning
assert_match(/delegator does not forward private method #exit\!/, err)
assert_empty(err)
assert_match(/RUBY_PLATFORM/, out)
end
end
Expand Down

0 comments on commit 372bc59

Please sign in to comment.