From 77f4fdb59519153554c6ea09a2a5eeb6350564c0 Mon Sep 17 00:00:00 2001 From: Stan Lo Date: Sat, 6 Jan 2024 15:06:35 +0000 Subject: [PATCH 1/3] Remove unnecessary code from the exit command's implementation 1. The parameters of `IRB.irb_exit` were never used. But there are some libraries seem to call it with arguments + it's declared on the top-level IRB constant. So I changed the params to anonymous splat instead of removing them. 2. `Context#exit` was completely unnecessary as `IRB.irb_exit` doesn't use the `@irb` instance it passes. And since it's (or should be treated as) a private method, I simply removed it. 3. The `exit` command doesn't use the status argument it receives at all. But to avoid raising errors on usages like `exit 1`, I changed the argument to anonymous splat instead removing it. --- lib/irb.rb | 4 ++-- lib/irb/context.rb | 8 -------- lib/irb/extend-command.rb | 6 ++++-- 3 files changed, 6 insertions(+), 12 deletions(-) diff --git a/lib/irb.rb b/lib/irb.rb index daa0d64f2..4de8dda07 100644 --- a/lib/irb.rb +++ b/lib/irb.rb @@ -886,8 +886,8 @@ def IRB.start(ap_path = nil) end # Quits irb - def IRB.irb_exit(irb, ret) - throw :IRB_EXIT, ret + def IRB.irb_exit(*) + throw :IRB_EXIT end # Aborts then interrupts irb. diff --git a/lib/irb/context.rb b/lib/irb/context.rb index c3690fcac..10dab8617 100644 --- a/lib/irb/context.rb +++ b/lib/irb/context.rb @@ -573,14 +573,6 @@ def inspect_last_value # :nodoc: @inspect_method.inspect_value(@last_value) end - alias __exit__ exit - # Exits the current session, see IRB.irb_exit - def exit(ret = 0) - IRB.irb_exit(@irb, ret) - rescue UncaughtThrowError - super - end - NOPRINTING_IVARS = ["@last_value"] # :nodoc: NO_INSPECTING_IVARS = ["@irb", "@io"] # :nodoc: IDNAME_IVARS = ["@prompt_mode"] # :nodoc: diff --git a/lib/irb/extend-command.rb b/lib/irb/extend-command.rb index 072069d4c..59d7203eb 100644 --- a/lib/irb/extend-command.rb +++ b/lib/irb/extend-command.rb @@ -21,8 +21,10 @@ module ExtendCommandBundle # +ret+ is the optional signal or message to send to Context#exit # # Same as IRB.CurrentContext.exit. - def irb_exit(ret = 0) - irb_context.exit(ret) + def irb_exit(*) + IRB.irb_exit + rescue UncaughtThrowError + Kernel.exit end # Displays current configuration. From 65e2e6db462daa2039804dc6aef5d9977499ba4f Mon Sep 17 00:00:00 2001 From: Stan Lo Date: Sat, 6 Jan 2024 15:19:30 +0000 Subject: [PATCH 2/3] Make exit an actual command --- lib/irb/cmd/exit.rb | 22 ++++++++++++++++++++++ lib/irb/extend-command.rb | 20 ++++++-------------- 2 files changed, 28 insertions(+), 14 deletions(-) create mode 100644 lib/irb/cmd/exit.rb diff --git a/lib/irb/cmd/exit.rb b/lib/irb/cmd/exit.rb new file mode 100644 index 000000000..415e55533 --- /dev/null +++ b/lib/irb/cmd/exit.rb @@ -0,0 +1,22 @@ +# frozen_string_literal: true + +require_relative "nop" + +module IRB + # :stopdoc: + + module ExtendCommand + class Exit < Nop + category "IRB" + description "Exit the current irb session." + + def execute(*) + IRB.irb_exit + rescue UncaughtThrowError + Kernel.exit + end + end + end + + # :startdoc: +end diff --git a/lib/irb/extend-command.rb b/lib/irb/extend-command.rb index 59d7203eb..69d83080d 100644 --- a/lib/irb/extend-command.rb +++ b/lib/irb/extend-command.rb @@ -16,17 +16,6 @@ module ExtendCommandBundle # See #install_alias_method. OVERRIDE_ALL = 0x02 - # Quits the current irb context - # - # +ret+ is the optional signal or message to send to Context#exit - # - # Same as IRB.CurrentContext.exit. - def irb_exit(*) - IRB.irb_exit - rescue UncaughtThrowError - Kernel.exit - end - # Displays current configuration. # # Modifying the configuration is achieved by sending a message to IRB.conf. @@ -37,13 +26,16 @@ def irb_context @ALIASES = [ [:context, :irb_context, NO_OVERRIDE], [:conf, :irb_context, NO_OVERRIDE], - [:irb_quit, :irb_exit, OVERRIDE_PRIVATE_ONLY], - [:exit, :irb_exit, OVERRIDE_PRIVATE_ONLY], - [:quit, :irb_exit, OVERRIDE_PRIVATE_ONLY], ] @EXTEND_COMMANDS = [ + [ + :irb_exit, :Exit, "cmd/exit", + [:exit, OVERRIDE_PRIVATE_ONLY], + [:quit, OVERRIDE_PRIVATE_ONLY], + [:irb_quit, OVERRIDE_PRIVATE_ONLY], + ], [ :irb_current_working_workspace, :CurrentWorkingWorkspace, "cmd/chws", [:cwws, NO_OVERRIDE], From a64f7a40a02c42b1f25a6141be9978922c1b712b Mon Sep 17 00:00:00 2001 From: Stan Lo Date: Sat, 6 Jan 2024 15:20:41 +0000 Subject: [PATCH 3/3] Update readme --- README.md | 21 +++++++++++++-------- 1 file changed, 13 insertions(+), 8 deletions(-) diff --git a/README.md b/README.md index c6259fefb..d6fbe9e39 100644 --- a/README.md +++ b/README.md @@ -109,15 +109,9 @@ Hello World The following commands are available on IRB. You can get the same output from the `show_cmds` command. -``` -Workspace - cwws Show the current workspace. - chws Change the current workspace to an object. - workspaces Show workspaces. - pushws Push an object to the workspace stack. - popws Pop a workspace from the workspace stack. - +```txt IRB + exit Exit the current irb session. irb_load Load a Ruby file. irb_require Require a Ruby file. source Loads a given file in the current session. @@ -125,6 +119,13 @@ IRB show_cmds List all available commands and their description. history Shows the input history. `-g [query]` or `-G [query]` allows you to filter the output. +Workspace + cwws Show the current workspace. + chws Change the current workspace to an object. + workspaces Show workspaces. + pushws Push an object to the workspace stack. + popws Pop a workspace from the workspace stack. + Multi-irb (DEPRECATED) irb Start a child IRB. jobs List of current sessions. @@ -153,6 +154,10 @@ Context ls Show methods, constants, and variables. `-g [query]` or `-G [query]` allows you to filter out the output. show_source Show the source code of a given method or constant. whereami Show the source code around binding.irb again. + +Aliases + $ Alias for `show_source` + @ Alias for `whereami` ``` ## Debugging with IRB