irb 1.12.0 → 1.13.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (57) hide show
  1. checksums.yaml +4 -4
  2. data/README.md +3 -11
  3. data/Rakefile +10 -0
  4. data/irb.gemspec +1 -1
  5. data/lib/irb/cmd/nop.rb +1 -1
  6. data/lib/irb/color.rb +2 -2
  7. data/lib/irb/command/backtrace.rb +2 -6
  8. data/lib/irb/command/base.rb +7 -9
  9. data/lib/irb/command/break.rb +2 -6
  10. data/lib/irb/command/catch.rb +2 -6
  11. data/lib/irb/command/chws.rb +11 -5
  12. data/lib/irb/command/context.rb +16 -0
  13. data/lib/irb/command/continue.rb +2 -2
  14. data/lib/irb/command/debug.rb +8 -1
  15. data/lib/irb/command/delete.rb +2 -2
  16. data/lib/irb/command/disable_irb.rb +19 -0
  17. data/lib/irb/command/edit.rb +6 -13
  18. data/lib/irb/command/exit.rb +1 -3
  19. data/lib/irb/command/finish.rb +2 -2
  20. data/lib/irb/command/force_exit.rb +1 -3
  21. data/lib/irb/command/help.rb +8 -17
  22. data/lib/irb/command/history.rb +4 -6
  23. data/lib/irb/command/info.rb +2 -6
  24. data/lib/irb/command/internal_helpers.rb +27 -0
  25. data/lib/irb/command/irb_info.rb +2 -2
  26. data/lib/irb/command/load.rb +20 -3
  27. data/lib/irb/command/ls.rb +20 -10
  28. data/lib/irb/command/measure.rb +12 -6
  29. data/lib/irb/command/next.rb +2 -2
  30. data/lib/irb/command/pushws.rb +10 -5
  31. data/lib/irb/command/show_doc.rb +9 -18
  32. data/lib/irb/command/show_source.rb +5 -12
  33. data/lib/irb/command/step.rb +2 -2
  34. data/lib/irb/command/subirb.rb +28 -12
  35. data/lib/irb/command/whereami.rb +1 -1
  36. data/lib/irb/command.rb +8 -303
  37. data/lib/irb/completion.rb +16 -5
  38. data/lib/irb/context.rb +21 -19
  39. data/lib/irb/default_commands.rb +260 -0
  40. data/lib/irb/ext/change-ws.rb +3 -5
  41. data/lib/irb/ext/multi-irb.rb +4 -4
  42. data/lib/irb/ext/workspaces.rb +3 -4
  43. data/lib/irb/help.rb +1 -1
  44. data/lib/irb/helper_method/base.rb +16 -0
  45. data/lib/irb/helper_method/conf.rb +11 -0
  46. data/lib/irb/helper_method.rb +29 -0
  47. data/lib/irb/history.rb +6 -3
  48. data/lib/irb/init.rb +60 -44
  49. data/lib/irb/input-method.rb +18 -10
  50. data/lib/irb/lc/error.rb +0 -5
  51. data/lib/irb/lc/ja/error.rb +0 -5
  52. data/lib/irb/lc/ja/help-message +10 -0
  53. data/lib/irb/statement.rb +5 -27
  54. data/lib/irb/version.rb +2 -2
  55. data/lib/irb/workspace.rb +18 -2
  56. data/lib/irb.rb +675 -624
  57. metadata +12 -5
data/lib/irb.rb CHANGED
@@ -1,5 +1,6 @@
1
1
  # frozen_string_literal: true
2
- #
2
+
3
+ # :markup: markdown
3
4
  # irb.rb - irb main module
4
5
  # by Keiju ISHITSUKA(keiju@ruby-lang.org)
5
6
  #
@@ -9,7 +10,7 @@ require "reline"
9
10
 
10
11
  require_relative "irb/init"
11
12
  require_relative "irb/context"
12
- require_relative "irb/command"
13
+ require_relative "irb/default_commands"
13
14
 
14
15
  require_relative "irb/ruby-lex"
15
16
  require_relative "irb/statement"
@@ -22,545 +23,550 @@ require_relative "irb/easter-egg"
22
23
  require_relative "irb/debug"
23
24
  require_relative "irb/pager"
24
25
 
25
- # == \IRB
26
+ # ## IRB
26
27
  #
27
- # \Module \IRB ("Interactive Ruby") provides a shell-like interface
28
- # that supports user interaction with the Ruby interpreter.
28
+ # Module IRB ("Interactive Ruby") provides a shell-like interface that supports
29
+ # user interaction with the Ruby interpreter.
29
30
  #
30
- # It operates as a <i>read-eval-print loop</i>
31
- # ({REPL}[https://en.wikipedia.org/wiki/Read%E2%80%93eval%E2%80%93print_loop])
31
+ # It operates as a *read-eval-print loop*
32
+ # ([REPL](https://en.wikipedia.org/wiki/Read%E2%80%93eval%E2%80%93print_loop))
32
33
  # that:
33
34
  #
34
- # - <b>_Reads_</b> each character as you type.
35
- # You can modify the \IRB context to change the way input works.
36
- # See {Input}[rdoc-ref:IRB@Input].
37
- # - <b>_Evaluates_</b> the code each time it has read a syntactically complete passage.
38
- # - <b>_Prints_</b> after evaluating.
39
- # You can modify the \IRB context to change the way output works.
40
- # See {Output}[rdoc-ref:IRB@Output].
35
+ # * ***Reads*** each character as you type. You can modify the IRB context to
36
+ # change the way input works. See [Input](rdoc-ref:IRB@Input).
37
+ # * ***Evaluates*** the code each time it has read a syntactically complete
38
+ # passage.
39
+ # * ***Prints*** after evaluating. You can modify the IRB context to change
40
+ # the way output works. See [Output](rdoc-ref:IRB@Output).
41
+ #
41
42
  #
42
43
  # Example:
43
44
  #
44
- # $ irb
45
- # irb(main):001> File.basename(Dir.pwd)
46
- # => "irb"
47
- # irb(main):002> Dir.entries('.').size
48
- # => 25
49
- # irb(main):003* Dir.entries('.').select do |entry|
50
- # irb(main):004* entry.start_with?('R')
51
- # irb(main):005> end
52
- # => ["README.md", "Rakefile"]
45
+ # $ irb
46
+ # irb(main):001> File.basename(Dir.pwd)
47
+ # => "irb"
48
+ # irb(main):002> Dir.entries('.').size
49
+ # => 25
50
+ # irb(main):003* Dir.entries('.').select do |entry|
51
+ # irb(main):004* entry.start_with?('R')
52
+ # irb(main):005> end
53
+ # => ["README.md", "Rakefile"]
54
+ #
55
+ # The typed input may also include [\IRB-specific
56
+ # commands](rdoc-ref:IRB@IRB-Specific+Commands).
53
57
  #
54
- # The typed input may also include
55
- # {\IRB-specific commands}[rdoc-ref:IRB@IRB-Specific+Commands].
58
+ # As seen above, you can start IRB by using the shell command `irb`.
56
59
  #
57
- # As seen above, you can start \IRB by using the shell command +irb+.
60
+ # You can stop an IRB session by typing command `exit`:
58
61
  #
59
- # You can stop an \IRB session by typing command +exit+:
62
+ # irb(main):006> exit
63
+ # $
60
64
  #
61
- # irb(main):006> exit
62
- # $
65
+ # At that point, IRB calls any hooks found in array `IRB.conf[:AT_EXIT]`, then
66
+ # exits.
63
67
  #
64
- # At that point, \IRB calls any hooks found in array <tt>IRB.conf[:AT_EXIT]</tt>,
65
- # then exits.
68
+ # ## Startup
66
69
  #
67
- # == Startup
70
+ # At startup, IRB:
68
71
  #
69
- # At startup, \IRB:
72
+ # 1. Interprets (as Ruby code) the content of the [configuration
73
+ # file](rdoc-ref:IRB@Configuration+File) (if given).
74
+ # 2. Constructs the initial session context from [hash
75
+ # IRB.conf](rdoc-ref:IRB@Hash+IRB.conf) and from default values; the hash
76
+ # content may have been affected by [command-line
77
+ # options](rdoc-ref:IB@Command-Line+Options), and by direct assignments in
78
+ # the configuration file.
79
+ # 3. Assigns the context to variable `conf`.
80
+ # 4. Assigns command-line arguments to variable `ARGV`.
81
+ # 5. Prints the [prompt](rdoc-ref:IRB@Prompt+and+Return+Formats).
82
+ # 6. Puts the content of the [initialization
83
+ # script](rdoc-ref:IRB@Initialization+Script) onto the IRB shell, just as if
84
+ # it were user-typed commands.
70
85
  #
71
- # 1. Interprets (as Ruby code) the content of the
72
- # {configuration file}[rdoc-ref:IRB@Configuration+File] (if given).
73
- # 1. Constructs the initial session context
74
- # from {hash IRB.conf}[rdoc-ref:IRB@Hash+IRB.conf] and from default values;
75
- # the hash content may have been affected
76
- # by {command-line options}[rdoc-ref:IB@Command-Line+Options],
77
- # and by direct assignments in the configuration file.
78
- # 1. Assigns the context to variable +conf+.
79
- # 1. Assigns command-line arguments to variable <tt>ARGV</tt>.
80
- # 1. Prints the {prompt}[rdoc-ref:IRB@Prompt+and+Return+Formats].
81
- # 1. Puts the content of the
82
- # {initialization script}[rdoc-ref:IRB@Initialization+Script]
83
- # onto the \IRB shell, just as if it were user-typed commands.
84
86
  #
85
- # === The Command Line
87
+ # ### The Command Line
86
88
  #
87
- # On the command line, all options precede all arguments;
88
- # the first item that is not recognized as an option is treated as an argument,
89
- # as are all items that follow.
89
+ # On the command line, all options precede all arguments; the first item that is
90
+ # not recognized as an option is treated as an argument, as are all items that
91
+ # follow.
90
92
  #
91
- # ==== Command-Line Options
93
+ # #### Command-Line Options
92
94
  #
93
- # Many command-line options affect entries in hash <tt>IRB.conf</tt>,
94
- # which in turn affect the initial configuration of the \IRB session.
95
+ # Many command-line options affect entries in hash `IRB.conf`, which in turn
96
+ # affect the initial configuration of the IRB session.
95
97
  #
96
98
  # Details of the options are described in the relevant subsections below.
97
99
  #
98
- # A cursory list of the \IRB command-line options
99
- # may be seen in the {help message}[https://raw.githubusercontent.com/ruby/irb/master/lib/irb/lc/help-message],
100
- # which is also displayed if you use command-line option <tt>--help</tt>.
100
+ # A cursory list of the IRB command-line options may be seen in the [help
101
+ # message](https://raw.githubusercontent.com/ruby/irb/master/lib/irb/lc/help-message),
102
+ # which is also displayed if you use command-line option `--help`.
101
103
  #
102
104
  # If you are interested in a specific option, consult the
103
- # {index}[rdoc-ref:doc/irb/indexes.md@Index+of+Command-Line+Options].
105
+ # [index](rdoc-ref:doc/irb/indexes.md@Index+of+Command-Line+Options).
104
106
  #
105
- # ==== Command-Line Arguments
107
+ # #### Command-Line Arguments
106
108
  #
107
- # Command-line arguments are passed to \IRB in array +ARGV+:
109
+ # Command-line arguments are passed to IRB in array `ARGV`:
108
110
  #
109
- # $ irb --noscript Foo Bar Baz
110
- # irb(main):001> ARGV
111
- # => ["Foo", "Bar", "Baz"]
112
- # irb(main):002> exit
113
- # $
111
+ # $ irb --noscript Foo Bar Baz
112
+ # irb(main):001> ARGV
113
+ # => ["Foo", "Bar", "Baz"]
114
+ # irb(main):002> exit
115
+ # $
114
116
  #
115
- # Command-line option <tt>--</tt> causes everything that follows
116
- # to be treated as arguments, even those that look like options:
117
+ # Command-line option `--` causes everything that follows to be treated as
118
+ # arguments, even those that look like options:
117
119
  #
118
- # $ irb --noscript -- --noscript -- Foo Bar Baz
119
- # irb(main):001> ARGV
120
- # => ["--noscript", "--", "Foo", "Bar", "Baz"]
121
- # irb(main):002> exit
122
- # $
120
+ # $ irb --noscript -- --noscript -- Foo Bar Baz
121
+ # irb(main):001> ARGV
122
+ # => ["--noscript", "--", "Foo", "Bar", "Baz"]
123
+ # irb(main):002> exit
124
+ # $
123
125
  #
124
- # === Configuration File
126
+ # ### Configuration File
125
127
  #
126
- # You can initialize \IRB via a <i>configuration file</i>.
128
+ # You can initialize IRB via a *configuration file*.
127
129
  #
128
- # If command-line option <tt>-f</tt> is given,
129
- # no configuration file is looked for.
130
+ # If command-line option `-f` is given, no configuration file is looked for.
130
131
  #
131
- # Otherwise, \IRB reads and interprets a configuration file
132
- # if one is available.
132
+ # Otherwise, IRB reads and interprets a configuration file if one is available.
133
133
  #
134
134
  # The configuration file can contain any Ruby code, and can usefully include
135
135
  # user code that:
136
136
  #
137
- # - Can then be debugged in \IRB.
138
- # - Configures \IRB itself.
139
- # - Requires or loads files.
137
+ # * Can then be debugged in IRB.
138
+ # * Configures IRB itself.
139
+ # * Requires or loads files.
140
+ #
140
141
  #
141
142
  # The path to the configuration file is the first found among:
142
143
  #
143
- # - The value of variable <tt>$IRBRC</tt>, if defined.
144
- # - The value of variable <tt>$XDG_CONFIG_HOME/irb/irbrc</tt>, if defined.
145
- # - File <tt>$HOME/.irbrc</tt>, if it exists.
146
- # - File <tt>$HOME/.config/irb/irbrc</tt>, if it exists.
147
- # - File +.config/irb/irbrc+ in the current directory, if it exists.
148
- # - File +.irbrc+ in the current directory, if it exists.
149
- # - File +irb.rc+ in the current directory, if it exists.
150
- # - File +_irbrc+ in the current directory, if it exists.
151
- # - File <tt>$irbrc</tt> in the current directory, if it exists.
144
+ # * The value of variable `$IRBRC`, if defined.
145
+ # * The value of variable `$XDG_CONFIG_HOME/irb/irbrc`, if defined.
146
+ # * File `$HOME/.irbrc`, if it exists.
147
+ # * File `$HOME/.config/irb/irbrc`, if it exists.
148
+ # * File `.irbrc` in the current directory, if it exists.
149
+ # * File `irb.rc` in the current directory, if it exists.
150
+ # * File `_irbrc` in the current directory, if it exists.
151
+ # * File `$irbrc` in the current directory, if it exists.
152
+ #
152
153
  #
153
154
  # If the search fails, there is no configuration file.
154
155
  #
155
- # If the search succeeds, the configuration file is read as Ruby code,
156
- # and so can contain any Ruby programming you like.
156
+ # If the search succeeds, the configuration file is read as Ruby code, and so
157
+ # can contain any Ruby programming you like.
158
+ #
159
+ # Method `conf.rc?` returns `true` if a configuration file was read, `false`
160
+ # otherwise. Hash entry `IRB.conf[:RC]` also contains that value.
157
161
  #
158
- # \Method <tt>conf.rc?</tt> returns +true+ if a configuration file was read,
159
- # +false+ otherwise.
160
- # \Hash entry <tt>IRB.conf[:RC]</tt> also contains that value.
162
+ # ### Hash `IRB.conf`
161
163
  #
162
- # === \Hash <tt>IRB.conf</tt>
164
+ # The initial entries in hash `IRB.conf` are determined by:
163
165
  #
164
- # The initial entries in hash <tt>IRB.conf</tt> are determined by:
166
+ # * Default values.
167
+ # * Command-line options, which may override defaults.
168
+ # * Direct assignments in the configuration file.
165
169
  #
166
- # - Default values.
167
- # - Command-line options, which may override defaults.
168
- # - Direct assignments in the configuration file.
169
170
  #
170
- # You can see the hash by typing <tt>IRB.conf</tt>.
171
+ # You can see the hash by typing `IRB.conf`.
171
172
  #
172
- # Details of the entries' meanings are described in the relevant subsections below.
173
+ # Details of the entries' meanings are described in the relevant subsections
174
+ # below.
173
175
  #
174
176
  # If you are interested in a specific entry, consult the
175
- # {index}[rdoc-ref:doc/irb/indexes.md@Index+of+IRB.conf+Entries].
177
+ # [index](rdoc-ref:doc/irb/indexes.md@Index+of+IRB.conf+Entries).
178
+ #
179
+ # ### Notes on Initialization Precedence
176
180
  #
177
- # === Notes on Initialization Precedence
181
+ # * Any conflict between an entry in hash `IRB.conf` and a command-line option
182
+ # is resolved in favor of the hash entry.
183
+ # * Hash `IRB.conf` affects the context only once, when the configuration file
184
+ # is interpreted; any subsequent changes to it do not affect the context and
185
+ # are therefore essentially meaningless.
178
186
  #
179
- # - Any conflict between an entry in hash <tt>IRB.conf</tt> and a command-line option
180
- # is resolved in favor of the hash entry.
181
- # - \Hash <tt>IRB.conf</tt> affects the context only once,
182
- # when the configuration file is interpreted;
183
- # any subsequent changes to it do not affect the context
184
- # and are therefore essentially meaningless.
185
187
  #
186
- # === Initialization Script
188
+ # ### Initialization Script
187
189
  #
188
- # By default, the first command-line argument (after any options)
189
- # is the path to a Ruby initialization script.
190
+ # By default, the first command-line argument (after any options) is the path to
191
+ # a Ruby initialization script.
190
192
  #
191
- # \IRB reads the initialization script and puts its content onto the \IRB shell,
193
+ # IRB reads the initialization script and puts its content onto the IRB shell,
192
194
  # just as if it were user-typed commands.
193
195
  #
194
- # Command-line option <tt>--noscript</tt> causes the first command-line argument
195
- # to be treated as an ordinary argument (instead of an initialization script);
196
- # <tt>--script</tt> is the default.
196
+ # Command-line option `--noscript` causes the first command-line argument to be
197
+ # treated as an ordinary argument (instead of an initialization script);
198
+ # `--script` is the default.
197
199
  #
198
- # == Input
200
+ # ## Input
199
201
  #
200
- # This section describes the features that allow you to change
201
- # the way \IRB input works;
202
- # see also {Input and Output}[rdoc-ref:IRB@Input+and+Output].
202
+ # This section describes the features that allow you to change the way IRB input
203
+ # works; see also [Input and Output](rdoc-ref:IRB@Input+and+Output).
203
204
  #
204
- # === Input Command History
205
+ # ### Input Command History
205
206
  #
206
- # By default, \IRB stores a history of up to 1000 input commands in a
207
- # file named <tt>.irb_history</tt>. The history file will be in the same directory
208
- # as the {configuration file}[rdoc-ref:IRB@Configuration+File] if one is found, or
209
- # in <tt>~/</tt> otherwise.
207
+ # By default, IRB stores a history of up to 1000 input commands in a file named
208
+ # `.irb_history`. The history file will be in the same directory as the
209
+ # [configuration file](rdoc-ref:IRB@Configuration+File) if one is found, or in
210
+ # `~/` otherwise.
210
211
  #
211
- # A new \IRB session creates the history file if it does not exist,
212
- # and appends to the file if it does exist.
212
+ # A new IRB session creates the history file if it does not exist, and appends
213
+ # to the file if it does exist.
213
214
  #
214
215
  # You can change the filepath by adding to your configuration file:
215
- # <tt>IRB.conf[:HISTORY_FILE] = _filepath_</tt>,
216
- # where _filepath_ is a string filepath.
216
+ # `IRB.conf[:HISTORY_FILE] = *filepath*`, where *filepath* is a string filepath.
217
217
  #
218
- # During the session, method <tt>conf.history_file</tt> returns the filepath,
219
- # and method <tt>conf.history_file = <i>new_filepath</i></tt>
220
- # copies the history to the file at <i>new_filepath</i>,
221
- # which becomes the history file for the session.
218
+ # During the session, method `conf.history_file` returns the filepath, and
219
+ # method `conf.history_file = *new_filepath*` copies the history to the file at
220
+ # *new_filepath*, which becomes the history file for the session.
222
221
  #
223
- # You can change the number of commands saved by adding to your configuration file:
224
- # <tt>IRB.conf[:SAVE_HISTORY] = _n_</tt>,
225
- # where _n_ is one of:
222
+ # You can change the number of commands saved by adding to your configuration
223
+ # file: `IRB.conf[:SAVE_HISTORY] = *n*`, wheHISTORY_FILEre *n* is one of:
226
224
  #
227
- # - Positive integer: the number of commands to be saved,
228
- # - Zero: all commands are to be saved.
229
- # - +nil+: no commands are to be saved,.
225
+ # * Positive integer: the number of commands to be saved,
226
+ # * Zero: all commands are to be saved.
227
+ # * `nil`: no commands are to be saved,.
230
228
  #
231
- # During the session, you can use
232
- # methods <tt>conf.save_history</tt> or <tt>conf.save_history=</tt>
233
- # to retrieve or change the count.
234
229
  #
235
- # === Command Aliases
230
+ # During the session, you can use methods `conf.save_history` or
231
+ # `conf.save_history=` to retrieve or change the count.
236
232
  #
237
- # By default, \IRB defines several command aliases:
233
+ # ### Command Aliases
238
234
  #
239
- # irb(main):001> conf.command_aliases
240
- # => {:"$"=>:show_source, :"@"=>:whereami}
235
+ # By default, IRB defines several command aliases:
236
+ #
237
+ # irb(main):001> conf.command_aliases
238
+ # => {:"$"=>:show_source, :"@"=>:whereami}
241
239
  #
242
240
  # You can change the initial aliases in the configuration file with:
243
241
  #
244
- # IRB.conf[:COMMAND_ALIASES] = {foo: :show_source, bar: :whereami}
242
+ # IRB.conf[:COMMAND_ALIASES] = {foo: :show_source, bar: :whereami}
245
243
  #
246
- # You can replace the current aliases at any time
247
- # with configuration method <tt>conf.command_aliases=</tt>;
248
- # Because <tt>conf.command_aliases</tt> is a hash,
249
- # you can modify it.
244
+ # You can replace the current aliases at any time with configuration method
245
+ # `conf.command_aliases=`; Because `conf.command_aliases` is a hash, you can
246
+ # modify it.
250
247
  #
251
- # === End-of-File
248
+ # ### End-of-File
252
249
  #
253
- # By default, <tt>IRB.conf[:IGNORE_EOF]</tt> is +false+,
254
- # which means that typing the end-of-file character <tt>Ctrl-D</tt>
255
- # causes the session to exit.
250
+ # By default, `IRB.conf[:IGNORE_EOF]` is `false`, which means that typing the
251
+ # end-of-file character `Ctrl-D` causes the session to exit.
256
252
  #
257
- # You can reverse that behavior by adding <tt>IRB.conf[:IGNORE_EOF] = true</tt>
258
- # to the configuration file.
253
+ # You can reverse that behavior by adding `IRB.conf[:IGNORE_EOF] = true` to the
254
+ # configuration file.
259
255
  #
260
- # During the session, method <tt>conf.ignore_eof?</tt> returns the setting,
261
- # and method <tt>conf.ignore_eof = _boolean_</tt> sets it.
256
+ # During the session, method `conf.ignore_eof?` returns the setting, and method
257
+ # `conf.ignore_eof = *boolean*` sets it.
262
258
  #
263
- # === SIGINT
259
+ # ### SIGINT
264
260
  #
265
- # By default, <tt>IRB.conf[:IGNORE_SIGINT]</tt> is +true+,
266
- # which means that typing the interrupt character <tt>Ctrl-C</tt>
267
- # causes the session to exit.
261
+ # By default, `IRB.conf[:IGNORE_SIGINT]` is `true`, which means that typing the
262
+ # interrupt character `Ctrl-C` causes the session to exit.
268
263
  #
269
- # You can reverse that behavior by adding <tt>IRB.conf[:IGNORE_SIGING] = false</tt>
270
- # to the configuration file.
264
+ # You can reverse that behavior by adding `IRB.conf[:IGNORE_SIGING] = false` to
265
+ # the configuration file.
271
266
  #
272
- # During the session, method <tt>conf.ignore_siging?</tt> returns the setting,
273
- # and method <tt>conf.ignore_sigint = _boolean_</tt> sets it.
267
+ # During the session, method `conf.ignore_siging?` returns the setting, and
268
+ # method `conf.ignore_sigint = *boolean*` sets it.
274
269
  #
275
- # === Automatic Completion
270
+ # ### Automatic Completion
276
271
  #
277
- # By default, \IRB enables
278
- # {automatic completion}[https://en.wikipedia.org/wiki/Autocomplete#In_command-line_interpreters]:
272
+ # By default, IRB enables [automatic
273
+ # completion](https://en.wikipedia.org/wiki/Autocomplete#In_command-line_interpr
274
+ # eters):
279
275
  #
280
276
  # You can disable it by either of these:
281
277
  #
282
- # - Adding <tt>IRB.conf[:USE_AUTOCOMPLETE] = false</tt> to the configuration file.
283
- # - Giving command-line option <tt>--noautocomplete</tt>
284
- # (<tt>--autocomplete</tt> is the default).
278
+ # * Adding `IRB.conf[:USE_AUTOCOMPLETE] = false` to the configuration file.
279
+ # * Giving command-line option `--noautocomplete` (`--autocomplete` is the
280
+ # default).
281
+ #
285
282
  #
286
- # \Method <tt>conf.use_autocomplete?</tt> returns +true+
287
- # if automatic completion is enabled, +false+ otherwise.
283
+ # Method `conf.use_autocomplete?` returns `true` if automatic completion is
284
+ # enabled, `false` otherwise.
288
285
  #
289
286
  # The setting may not be changed during the session.
290
287
  #
291
- # === Automatic Indentation
288
+ # ### Automatic Indentation
292
289
  #
293
- # By default, \IRB automatically indents lines of code to show structure
294
- # (e.g., it indent the contents of a block).
290
+ # By default, IRB automatically indents lines of code to show structure (e.g.,
291
+ # it indent the contents of a block).
295
292
  #
296
- # The current setting is returned
297
- # by the configuration method <tt>conf.auto_indent_mode</tt>.
293
+ # The current setting is returned by the configuration method
294
+ # `conf.auto_indent_mode`.
298
295
  #
299
- # The default initial setting is +true+:
296
+ # The default initial setting is `true`:
300
297
  #
301
- # irb(main):001> conf.auto_indent_mode
302
- # => true
303
- # irb(main):002* Dir.entries('.').select do |entry|
304
- # irb(main):003* entry.start_with?('R')
305
- # irb(main):004> end
306
- # => ["README.md", "Rakefile"]
298
+ # irb(main):001> conf.auto_indent_mode
299
+ # => true
300
+ # irb(main):002* Dir.entries('.').select do |entry|
301
+ # irb(main):003* entry.start_with?('R')
302
+ # irb(main):004> end
303
+ # => ["README.md", "Rakefile"]
307
304
  #
308
- # You can change the initial setting in the
309
- # configuration file with:
305
+ # You can change the initial setting in the configuration file with:
310
306
  #
311
- # IRB.conf[:AUTO_INDENT] = false
307
+ # IRB.conf[:AUTO_INDENT] = false
312
308
  #
313
- # Note that the _current_ setting <i>may not</i> be changed in the \IRB session.
309
+ # Note that the *current* setting *may not* be changed in the IRB session.
314
310
  #
315
- # === Input \Method
311
+ # ### Input Method
316
312
  #
317
- # The \IRB input method determines how command input is to be read;
318
- # by default, the input method for a session is IRB::RelineInputMethod.
313
+ # The IRB input method determines how command input is to be read; by default,
314
+ # the input method for a session is IRB::RelineInputMethod.
319
315
  #
320
316
  # You can set the input method by:
321
317
  #
322
- # - Adding to the configuration file:
318
+ # * Adding to the configuration file:
319
+ #
320
+ # * `IRB.conf[:USE_SINGLELINE] = true` or `IRB.conf[:USE_MULTILINE]=
321
+ # false` sets the input method to IRB::ReadlineInputMethod.
322
+ # * `IRB.conf[:USE_SINGLELINE] = false` or `IRB.conf[:USE_MULTILINE] =
323
+ # true` sets the input method to IRB::RelineInputMethod.
324
+ #
325
+ #
326
+ # * Giving command-line options:
327
+ #
328
+ # * `--singleline` or `--nomultiline` sets the input method to
329
+ # IRB::ReadlineInputMethod.
330
+ # * `--nosingleline` or `--multiline` sets the input method to
331
+ # IRB::RelineInputMethod.
323
332
  #
324
- # - <tt>IRB.conf[:USE_SINGLELINE] = true</tt>
325
- # or <tt>IRB.conf[:USE_MULTILINE]= false</tt>
326
- # sets the input method to IRB::ReadlineInputMethod.
327
- # - <tt>IRB.conf[:USE_SINGLELINE] = false</tt>
328
- # or <tt>IRB.conf[:USE_MULTILINE] = true</tt>
329
- # sets the input method to IRB::RelineInputMethod.
330
333
  #
331
- # - Giving command-line options:
332
334
  #
333
- # - <tt>--singleline</tt>
334
- # or <tt>--nomultiline</tt>
335
- # sets the input method to IRB::ReadlineInputMethod.
336
- # - <tt>--nosingleline</tt>
337
- # or <tt>--multiline/tt>
338
- # sets the input method to IRB::RelineInputMethod.
335
+ # Method `conf.use_multiline?` and its synonym `conf.use_reline` return:
339
336
  #
340
- # \Method <tt>conf.use_multiline?</tt>
341
- # and its synonym <tt>conf.use_reline</tt> return:
337
+ # * `true` if option `--multiline` was given.
338
+ # * `false` if option `--nomultiline` was given.
339
+ # * `nil` if neither was given.
342
340
  #
343
- # - +true+ if option <tt>--multiline</tt> was given.
344
- # - +false+ if option <tt>--nomultiline</tt> was given.
345
- # - +nil+ if neither was given.
346
341
  #
347
- # \Method <tt>conf.use_singleline?</tt>
348
- # and its synonym <tt>conf.use_readline</tt> return:
342
+ # Method `conf.use_singleline?` and its synonym `conf.use_readline` return:
349
343
  #
350
- # - +true+ if option <tt>--singleline</tt> was given.
351
- # - +false+ if option <tt>--nosingleline</tt> was given.
352
- # - +nil+ if neither was given.
344
+ # * `true` if option `--singleline` was given.
345
+ # * `false` if option `--nosingleline` was given.
346
+ # * `nil` if neither was given.
353
347
  #
354
- # == Output
355
348
  #
356
- # This section describes the features that allow you to change
357
- # the way \IRB output works;
358
- # see also {Input and Output}[rdoc-ref:IRB@Input+and+Output].
349
+ # ## Output
359
350
  #
360
- # === Return-Value Printing (Echoing)
351
+ # This section describes the features that allow you to change the way IRB
352
+ # output works; see also [Input and Output](rdoc-ref:IRB@Input+and+Output).
361
353
  #
362
- # By default, \IRB prints (echoes) the values returned by all input commands.
354
+ # ### Return-Value Printing (Echoing)
355
+ #
356
+ # By default, IRB prints (echoes) the values returned by all input commands.
363
357
  #
364
358
  # You can change the initial behavior and suppress all echoing by:
365
359
  #
366
- # - Adding to the configuration file: <tt>IRB.conf[:ECHO] = false</tt>.
367
- # (The default value for this entry is +nil+, which means the same as +true+.)
368
- # - Giving command-line option <tt>--noecho</tt>.
369
- # (The default is <tt>--echo</tt>.)
360
+ # * Adding to the configuration file: `IRB.conf[:ECHO] = false`. (The default
361
+ # value for this entry is `nil`, which means the same as `true`.)
362
+ # * Giving command-line option `--noecho`. (The default is `--echo`.)
363
+ #
364
+ #
365
+ # During the session, you can change the current setting with configuration
366
+ # method `conf.echo=` (set to `true` or `false`).
367
+ #
368
+ # As stated above, by default IRB prints the values returned by all input
369
+ # commands; but IRB offers special treatment for values returned by assignment
370
+ # statements, which may be:
371
+ #
372
+ # * Printed with truncation (to fit on a single line of output), which is the
373
+ # default; an ellipsis (`...` is suffixed, to indicate the truncation):
370
374
  #
371
- # During the session, you can change the current setting
372
- # with configuration method <tt>conf.echo=</tt> (set to +true+ or +false+).
375
+ # irb(main):001> x = 'abc' * 100
373
376
  #
374
- # As stated above, by default \IRB prints the values returned by all input commands;
375
- # but \IRB offers special treatment for values returned by assignment statements,
376
- # which may be:
377
377
  #
378
- # - Printed with truncation (to fit on a single line of output),
379
- # which is the default;
380
- # an ellipsis (<tt>...</tt> is suffixed, to indicate the truncation):
378
+ # > "abcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabc...
381
379
  #
382
- # irb(main):001> x = 'abc' * 100
383
- # => "abcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabc...
380
+ # * Printed in full (regardless of the length).
381
+ # * Suppressed (not printed at all)
384
382
  #
385
- # - Printed in full (regardless of the length).
386
- # - Suppressed (not printed at all)
387
383
  #
388
384
  # You can change the initial behavior by:
389
385
  #
390
- # - Adding to the configuration file: <tt>IRB.conf[:ECHO_ON_ASSIGNMENT] = false</tt>.
391
- # (The default value for this entry is +niL+, which means the same as +:truncate+.)
392
- # - Giving command-line option <tt>--noecho-on-assignment</tt>
393
- # or <tt>--echo-on-assignment</tt>.
394
- # (The default is <tt>--truncate-echo-on-assignment</tt>.)
386
+ # * Adding to the configuration file: `IRB.conf[:ECHO_ON_ASSIGNMENT] = false`.
387
+ # (The default value for this entry is `niL`, which means the same as
388
+ # `:truncate`.)
389
+ # * Giving command-line option `--noecho-on-assignment` or
390
+ # `--echo-on-assignment`. (The default is `--truncate-echo-on-assignment`.)
395
391
  #
396
- # During the session, you can change the current setting
397
- # with configuration method <tt>conf.echo_on_assignment=</tt>
398
- # (set to +true+, +false+, or +:truncate+).
399
392
  #
400
- # By default, \IRB formats returned values by calling method +inspect+.
393
+ # During the session, you can change the current setting with configuration
394
+ # method `conf.echo_on_assignment=` (set to `true`, `false`, or `:truncate`).
395
+ #
396
+ # By default, IRB formats returned values by calling method `inspect`.
401
397
  #
402
398
  # You can change the initial behavior by:
403
399
  #
404
- # - Adding to the configuration file: <tt>IRB.conf[:INSPECT_MODE] = false</tt>.
405
- # (The default value for this entry is +true+.)
406
- # - Giving command-line option <tt>--noinspect</tt>.
407
- # (The default is <tt>--inspect</tt>.)
400
+ # * Adding to the configuration file: `IRB.conf[:INSPECT_MODE] = false`. (The
401
+ # default value for this entry is `true`.)
402
+ # * Giving command-line option `--noinspect`. (The default is `--inspect`.)
403
+ #
408
404
  #
409
- # During the session, you can change the setting using method <tt>conf.inspect_mode=</tt>.
405
+ # During the session, you can change the setting using method
406
+ # `conf.inspect_mode=`.
410
407
  #
411
- # === Multiline Output
408
+ # ### Multiline Output
412
409
  #
413
- # By default, \IRB prefixes a newline to a multiline response.
410
+ # By default, IRB prefixes a newline to a multiline response.
414
411
  #
415
412
  # You can change the initial default value by adding to the configuration file:
416
413
  #
417
- # IRB.conf[:NEWLINE_BEFORE_MULTILINE_OUTPUT] = false
414
+ # IRB.conf[:NEWLINE_BEFORE_MULTILINE_OUTPUT] = false
418
415
  #
419
- # During a session, you can retrieve or set the value using
420
- # methods <tt>conf.newline_before_multiline_output?</tt>
421
- # and <tt>conf.newline_before_multiline_output=</tt>.
416
+ # During a session, you can retrieve or set the value using methods
417
+ # `conf.newline_before_multiline_output?` and
418
+ # `conf.newline_before_multiline_output=`.
422
419
  #
423
420
  # Examples:
424
421
  #
425
- # irb(main):001> conf.inspect_mode = false
426
- # => false
427
- # irb(main):002> "foo\nbar"
428
- # =>
429
- # foo
430
- # bar
431
- # irb(main):003> conf.newline_before_multiline_output = false
432
- # => false
433
- # irb(main):004> "foo\nbar"
434
- # => foo
435
- # bar
422
+ # irb(main):001> conf.inspect_mode = false
423
+ # => false
424
+ # irb(main):002> "foo\nbar"
425
+ # =>
426
+ # foo
427
+ # bar
428
+ # irb(main):003> conf.newline_before_multiline_output = false
429
+ # => false
430
+ # irb(main):004> "foo\nbar"
431
+ # => foo
432
+ # bar
433
+ #
434
+ # ### Evaluation History
436
435
  #
437
- # === Evaluation History
436
+ # By default, IRB saves no history of evaluations (returned values), and the
437
+ # related methods `conf.eval_history`, `_`, and `__` are undefined.
438
438
  #
439
- # By default, \IRB saves no history of evaluations (returned values),
440
- # and the related methods <tt>conf.eval_history</tt>, <tt>_</tt>,
441
- # and <tt>__</tt> are undefined.
439
+ # You can turn on that history, and set the maximum number of evaluations to be
440
+ # stored:
442
441
  #
443
- # You can turn on that history, and set the maximum number of evaluations to be stored:
442
+ # * In the configuration file: add `IRB.conf[:EVAL_HISTORY] = *n*`. (Examples
443
+ # below assume that we've added `IRB.conf[:EVAL_HISTORY] = 5`.)
444
+ # * In the session (at any time): `conf.eval_history = *n*`.
444
445
  #
445
- # - In the configuration file: add <tt>IRB.conf[:EVAL_HISTORY] = _n_</tt>.
446
- # (Examples below assume that we've added <tt>IRB.conf[:EVAL_HISTORY] = 5</tt>.)
447
- # - In the session (at any time): <tt>conf.eval_history = _n_</tt>.
448
446
  #
449
- # If +n+ is zero, all evaluation history is stored.
447
+ # If `n` is zero, all evaluation history is stored.
450
448
  #
451
449
  # Doing either of the above:
452
450
  #
453
- # - Sets the maximum size of the evaluation history;
454
- # defines method <tt>conf.eval_history</tt>,
455
- # which returns the maximum size +n+ of the evaluation history:
456
- #
457
- # irb(main):001> conf.eval_history = 5
458
- # => 5
459
- # irb(main):002> conf.eval_history
460
- # => 5
461
- #
462
- # - Defines variable <tt>_</tt>, which contains the most recent evaluation,
463
- # or +nil+ if none; same as method <tt>conf.last_value</tt>:
464
- #
465
- # irb(main):003> _
466
- # => 5
467
- # irb(main):004> :foo
468
- # => :foo
469
- # irb(main):005> :bar
470
- # => :bar
471
- # irb(main):006> _
472
- # => :bar
473
- # irb(main):007> _
474
- # => :bar
475
- #
476
- # - Defines variable <tt>__</tt>:
477
- #
478
- # - <tt>__</tt> unadorned: contains all evaluation history:
479
- #
480
- # irb(main):008> :foo
481
- # => :foo
482
- # irb(main):009> :bar
483
- # => :bar
484
- # irb(main):010> :baz
485
- # => :baz
486
- # irb(main):011> :bat
487
- # => :bat
488
- # irb(main):012> :bam
489
- # => :bam
490
- # irb(main):013> __
491
- # =>
492
- # 9 :bar
493
- # 10 :baz
494
- # 11 :bat
495
- # 12 :bam
496
- # irb(main):014> __
497
- # =>
498
- # 10 :baz
499
- # 11 :bat
500
- # 12 :bam
501
- # 13 ...self-history...
502
- #
503
- # Note that when the evaluation is multiline, it is displayed differently.
504
- #
505
- # - <tt>__[</tt>_m_<tt>]</tt>:
506
- #
507
- # - Positive _m_: contains the evaluation for the given line number,
508
- # or +nil+ if that line number is not in the evaluation history:
509
- #
510
- # irb(main):015> __[12]
511
- # => :bam
512
- # irb(main):016> __[1]
513
- # => nil
514
- #
515
- # - Negative _m_: contains the +mth+-from-end evaluation,
516
- # or +nil+ if that evaluation is not in the evaluation history:
517
- #
518
- # irb(main):017> __[-3]
519
- # => :bam
520
- # irb(main):018> __[-13]
521
- # => nil
522
- #
523
- # - Zero _m_: contains +nil+:
524
- #
525
- # irb(main):019> __[0]
526
- # => nil
527
- #
528
- # === Prompt and Return Formats
529
- #
530
- # By default, \IRB uses the prompt and return value formats
531
- # defined in its +:DEFAULT+ prompt mode.
532
- #
533
- # ==== The Default Prompt and Return Format
451
+ # * Sets the maximum size of the evaluation history; defines method
452
+ # `conf.eval_history`, which returns the maximum size `n` of the evaluation
453
+ # history:
454
+ #
455
+ # irb(main):001> conf.eval_history = 5
456
+ # => 5
457
+ # irb(main):002> conf.eval_history
458
+ # => 5
459
+ #
460
+ # * Defines variable `_`, which contains the most recent evaluation, or `nil`
461
+ # if none; same as method `conf.last_value`:
462
+ #
463
+ # irb(main):003> _
464
+ # => 5
465
+ # irb(main):004> :foo
466
+ # => :foo
467
+ # irb(main):005> :bar
468
+ # => :bar
469
+ # irb(main):006> _
470
+ # => :bar
471
+ # irb(main):007> _
472
+ # => :bar
473
+ #
474
+ # * Defines variable `__`:
475
+ #
476
+ # * `__` unadorned: contains all evaluation history:
477
+ #
478
+ # irb(main):008> :foo
479
+ # => :foo
480
+ # irb(main):009> :bar
481
+ # => :bar
482
+ # irb(main):010> :baz
483
+ # => :baz
484
+ # irb(main):011> :bat
485
+ # => :bat
486
+ # irb(main):012> :bam
487
+ # => :bam
488
+ # irb(main):013> __
489
+ # =>
490
+ # 9 :bar
491
+ # 10 :baz
492
+ # 11 :bat
493
+ # 12 :bam
494
+ # irb(main):014> __
495
+ # =>
496
+ # 10 :baz
497
+ # 11 :bat
498
+ # 12 :bam
499
+ # 13 ...self-history...
500
+ #
501
+ # Note that when the evaluation is multiline, it is displayed
502
+ # differently.
503
+ #
504
+ # * `__[`*m*`]`:
505
+ #
506
+ # * Positive *m*: contains the evaluation for the given line number,
507
+ # or `nil` if that line number is not in the evaluation history:
508
+ #
509
+ # irb(main):015> __[12]
510
+ # => :bam
511
+ # irb(main):016> __[1]
512
+ # => nil
513
+ #
514
+ # * Negative *m*: contains the `mth`-from-end evaluation, or `nil` if
515
+ # that evaluation is not in the evaluation history:
516
+ #
517
+ # irb(main):017> __[-3]
518
+ # => :bam
519
+ # irb(main):018> __[-13]
520
+ # => nil
521
+ #
522
+ # * Zero *m*: contains `nil`:
523
+ #
524
+ # irb(main):019> __[0]
525
+ # => nil
526
+ #
527
+ #
528
+ #
529
+ #
530
+ # ### Prompt and Return Formats
531
+ #
532
+ # By default, IRB uses the prompt and return value formats defined in its
533
+ # `:DEFAULT` prompt mode.
534
+ #
535
+ # #### The Default Prompt and Return Format
534
536
  #
535
537
  # The default prompt and return values look like this:
536
538
  #
537
- # irb(main):001> 1 + 1
538
- # => 2
539
- # irb(main):002> 2 + 2
540
- # => 4
539
+ # irb(main):001> 1 + 1
540
+ # => 2
541
+ # irb(main):002> 2 + 2
542
+ # => 4
541
543
  #
542
544
  # The prompt includes:
543
545
  #
544
- # - The name of the running program (<tt>irb</tt>);
545
- # see {IRB Name}[rdoc-ref:IRB@IRB+Name].
546
- # - The name of the current session (<tt>main</tt>);
547
- # See {IRB Sessions}[rdoc-ref:IRB@IRB+Sessions].
548
- # - A 3-digit line number (1-based).
546
+ # * The name of the running program (`irb`); see [IRB
547
+ # Name](rdoc-ref:IRB@IRB+Name).
548
+ # * The name of the current session (`main`); See [IRB
549
+ # Sessions](rdoc-ref:IRB@IRB+Sessions).
550
+ # * A 3-digit line number (1-based).
551
+ #
549
552
  #
550
553
  # The default prompt actually defines three formats:
551
554
  #
552
- # - One for most situations (as above):
555
+ # * One for most situations (as above):
553
556
  #
554
- # irb(main):003> Dir
555
- # => Dir
557
+ # irb(main):003> Dir
558
+ # => Dir
556
559
  #
557
- # - One for when the typed command is a statement continuation (adds trailing asterisk):
560
+ # * One for when the typed command is a statement continuation (adds trailing
561
+ # asterisk):
558
562
  #
559
- # irb(main):004* Dir.
563
+ # irb(main):004* Dir.
560
564
  #
561
- # - One for when the typed command is a string continuation (adds trailing single-quote):
565
+ # * One for when the typed command is a string continuation (adds trailing
566
+ # single-quote):
567
+ #
568
+ # irb(main):005' Dir.entries('.
562
569
  #
563
- # irb(main):005' Dir.entries('.
564
570
  #
565
571
  # You can see the prompt change as you type the characters in the following:
566
572
  #
@@ -569,258 +575,268 @@ require_relative "irb/pager"
569
575
  # irb(main):003> end
570
576
  # => ["README.md", "Rakefile"]
571
577
  #
572
- # ==== Pre-Defined Prompts
578
+ # #### Pre-Defined Prompts
573
579
  #
574
- # \IRB has several pre-defined prompts, stored in hash <tt>IRB.conf[:PROMPT]</tt>:
580
+ # IRB has several pre-defined prompts, stored in hash `IRB.conf[:PROMPT]`:
575
581
  #
576
- # irb(main):001> IRB.conf[:PROMPT].keys
577
- # => [:NULL, :DEFAULT, :CLASSIC, :SIMPLE, :INF_RUBY, :XMP]
582
+ # irb(main):001> IRB.conf[:PROMPT].keys
583
+ # => [:NULL, :DEFAULT, :CLASSIC, :SIMPLE, :INF_RUBY, :XMP]
578
584
  #
579
- # To see the full data for these, type <tt>IRB.conf[:PROMPT]</tt>.
585
+ # To see the full data for these, type `IRB.conf[:PROMPT]`.
580
586
  #
581
- # Most of these prompt definitions include specifiers that represent
582
- # values like the \IRB name, session name, and line number;
583
- # see {Prompt Specifiers}[rdoc-ref:IRB@Prompt+Specifiers].
587
+ # Most of these prompt definitions include specifiers that represent values like
588
+ # the IRB name, session name, and line number; see [Prompt
589
+ # Specifiers](rdoc-ref:IRB@Prompt+Specifiers).
584
590
  #
585
591
  # You can change the initial prompt and return format by:
586
592
  #
587
- # - Adding to the configuration file: <tt>IRB.conf[:PROMPT] = _mode_</tt>
588
- # where _mode_ is the symbol name of a prompt mode.
589
- # - Giving a command-line option:
593
+ # * Adding to the configuration file: `IRB.conf[:PROMPT] = *mode*` where
594
+ # *mode* is the symbol name of a prompt mode.
595
+ # * Giving a command-line option:
596
+ #
597
+ # * `--prompt *mode*`: sets the prompt mode to *mode*. where *mode* is the
598
+ # symbol name of a prompt mode.
599
+ # * `--simple-prompt` or `--sample-book-mode`: sets the prompt mode to
600
+ # `:SIMPLE`.
601
+ # * `--inf-ruby-mode`: sets the prompt mode to `:INF_RUBY` and suppresses
602
+ # both `--multiline` and `--singleline`.
603
+ # * `--noprompt`: suppresses prompting; does not affect echoing.
604
+ #
590
605
  #
591
- # - <tt>--prompt _mode_</tt>: sets the prompt mode to _mode_.
592
- # where _mode_ is the symbol name of a prompt mode.
593
- # - <tt>--simple-prompt</tt> or <tt>--sample-book-mode</tt>:
594
- # sets the prompt mode to +:SIMPLE+.
595
- # - <tt>--inf-ruby-mode</tt>: sets the prompt mode to +:INF_RUBY+
596
- # and suppresses both <tt>--multiline</tt> and <tt>--singleline</tt>.
597
- # - <tt>--noprompt</tt>: suppresses prompting; does not affect echoing.
598
606
  #
599
607
  # You can retrieve or set the current prompt mode with methods
600
608
  #
601
- # <tt>conf.prompt_mode</tt> and <tt>conf.prompt_mode=</tt>.
609
+ # `conf.prompt_mode` and `conf.prompt_mode=`.
602
610
  #
603
611
  # If you're interested in prompts and return formats other than the defaults,
604
612
  # you might experiment by trying some of the others.
605
613
  #
606
- # ==== Custom Prompts
614
+ # #### Custom Prompts
607
615
  #
608
- # You can also define custom prompts and return formats,
609
- # which may be done either in an \IRB session or in the configuration file.
616
+ # You can also define custom prompts and return formats, which may be done
617
+ # either in an IRB session or in the configuration file.
610
618
  #
611
- # A prompt in \IRB actually defines three prompts, as seen above.
612
- # For simple custom data, we'll make all three the same:
619
+ # A prompt in IRB actually defines three prompts, as seen above. For simple
620
+ # custom data, we'll make all three the same:
613
621
  #
614
- # irb(main):001* IRB.conf[:PROMPT][:MY_PROMPT] = {
615
- # irb(main):002* PROMPT_I: ': ',
616
- # irb(main):003* PROMPT_C: ': ',
617
- # irb(main):004* PROMPT_S: ': ',
618
- # irb(main):005* RETURN: '=> '
619
- # irb(main):006> }
620
- # => {:PROMPT_I=>": ", :PROMPT_C=>": ", :PROMPT_S=>": ", :RETURN=>"=> "}
622
+ # irb(main):001* IRB.conf[:PROMPT][:MY_PROMPT] = {
623
+ # irb(main):002* PROMPT_I: ': ',
624
+ # irb(main):003* PROMPT_C: ': ',
625
+ # irb(main):004* PROMPT_S: ': ',
626
+ # irb(main):005* RETURN: '=> '
627
+ # irb(main):006> }
628
+ # => {:PROMPT_I=>": ", :PROMPT_C=>": ", :PROMPT_S=>": ", :RETURN=>"=> "}
621
629
  #
622
- # If you define the custom prompt in the configuration file,
623
- # you can also make it the current prompt by adding:
630
+ # If you define the custom prompt in the configuration file, you can also make
631
+ # it the current prompt by adding:
624
632
  #
625
- # IRB.conf[:PROMPT_MODE] = :MY_PROMPT
633
+ # IRB.conf[:PROMPT_MODE] = :MY_PROMPT
626
634
  #
627
- # Regardless of where it's defined, you can make it the current prompt in a session:
635
+ # Regardless of where it's defined, you can make it the current prompt in a
636
+ # session:
628
637
  #
629
- # conf.prompt_mode = :MY_PROMPT
638
+ # conf.prompt_mode = :MY_PROMPT
630
639
  #
631
- # You can view or modify the current prompt data with various configuration methods:
640
+ # You can view or modify the current prompt data with various configuration
641
+ # methods:
632
642
  #
633
- # - <tt>conf.prompt_mode</tt>, <tt>conf.prompt_mode=</tt>.
634
- # - <tt>conf.prompt_c</tt>, <tt>conf.c=</tt>.
635
- # - <tt>conf.prompt_i</tt>, <tt>conf.i=</tt>.
636
- # - <tt>conf.prompt_s</tt>, <tt>conf.s=</tt>.
637
- # - <tt>conf.return_format</tt>, <tt>return_format=</tt>.
643
+ # * `conf.prompt_mode`, `conf.prompt_mode=`.
644
+ # * `conf.prompt_c`, `conf.c=`.
645
+ # * `conf.prompt_i`, `conf.i=`.
646
+ # * `conf.prompt_s`, `conf.s=`.
647
+ # * `conf.return_format`, `return_format=`.
638
648
  #
639
- # ==== Prompt Specifiers
640
649
  #
641
- # A prompt's definition can include specifiers for which certain values are substituted:
650
+ # #### Prompt Specifiers
642
651
  #
643
- # - <tt>%N</tt>: the name of the running program.
644
- # - <tt>%m</tt>: the value of <tt>self.to_s</tt>.
645
- # - <tt>%M</tt>: the value of <tt>self.inspect</tt>.
646
- # - <tt>%l</tt>: an indication of the type of string;
647
- # one of <tt>"</tt>, <tt>'</tt>, <tt>/</tt>, <tt>]</tt>.
648
- # - <tt><i>NN</i>i</tt>: Indentation level.
649
- # - <tt><i>NN</i>n</tt>: Line number.
650
- # - <tt>%%</tt>: Literal <tt>%</tt>.
652
+ # A prompt's definition can include specifiers for which certain values are
653
+ # substituted:
651
654
  #
652
- # === Verbosity
655
+ # * `%N`: the name of the running program.
656
+ # * `%m`: the value of `self.to_s`.
657
+ # * `%M`: the value of `self.inspect`.
658
+ # * `%l`: an indication of the type of string; one of `"`, `'`, `/`, `]`.
659
+ # * `%NNi`: Indentation level. NN is a 2-digit number that specifies the number
660
+ # of digits of the indentation level (03 will result in 001).
661
+ # * `%NNn`: Line number. NN is a 2-digit number that specifies the number
662
+ # of digits of the line number (03 will result in 001).
663
+ # * `%%`: Literal `%`.
653
664
  #
654
- # By default, \IRB verbosity is disabled, which means that output is smaller
665
+ #
666
+ # ### Verbosity
667
+ #
668
+ # By default, IRB verbosity is disabled, which means that output is smaller
655
669
  # rather than larger.
656
670
  #
657
671
  # You can enable verbosity by:
658
672
  #
659
- # - Adding to the configuration file: <tt>IRB.conf[:VERBOSE] = true</tt>
660
- # (the default is +nil+).
661
- # - Giving command-line options <tt>--verbose</tt>
662
- # (the default is <tt>--noverbose</tt>).
673
+ # * Adding to the configuration file: `IRB.conf[:VERBOSE] = true` (the default
674
+ # is `nil`).
675
+ # * Giving command-line options `--verbose` (the default is `--noverbose`).
676
+ #
663
677
  #
664
678
  # During a session, you can retrieve or set verbosity with methods
665
- # <tt>conf.verbose</tt> and <tt>conf.verbose=</tt>.
679
+ # `conf.verbose` and `conf.verbose=`.
666
680
  #
667
- # === Help
681
+ # ### Help
668
682
  #
669
- # Command-line option <tt>--version</tt> causes \IRB to print its help text
670
- # and exit.
683
+ # Command-line option `--version` causes IRB to print its help text and exit.
671
684
  #
672
- # === Version
685
+ # ### Version
673
686
  #
674
- # Command-line option <tt>--version</tt> causes \IRB to print its version text
675
- # and exit.
687
+ # Command-line option `--version` causes IRB to print its version text and exit.
676
688
  #
677
- # == Input and Output
689
+ # ## Input and Output
678
690
  #
679
- # === \Color Highlighting
691
+ # ### Color Highlighting
680
692
  #
681
- # By default, \IRB color highlighting is enabled, and is used for both:
693
+ # By default, IRB color highlighting is enabled, and is used for both:
694
+ #
695
+ # * Input: As you type, IRB reads the typed characters and highlights elements
696
+ # that it recognizes; it also highlights errors such as mismatched
697
+ # parentheses.
698
+ # * Output: IRB highlights syntactical elements.
682
699
  #
683
- # - Input: As you type, \IRB reads the typed characters and highlights
684
- # elements that it recognizes;
685
- # it also highlights errors such as mismatched parentheses.
686
- # - Output: \IRB highlights syntactical elements.
687
700
  #
688
701
  # You can disable color highlighting by:
689
702
  #
690
- # - Adding to the configuration file: <tt>IRB.conf[:USE_COLORIZE] = false</tt>
691
- # (the default value is +true+).
692
- # - Giving command-line option <tt>--nocolorize</tt>
703
+ # * Adding to the configuration file: `IRB.conf[:USE_COLORIZE] = false` (the
704
+ # default value is `true`).
705
+ # * Giving command-line option `--nocolorize`
706
+ #
693
707
  #
694
- # == Debugging
708
+ # ## Debugging
695
709
  #
696
- # Command-line option <tt>-d</tt> sets variables <tt>$VERBOSE</tt>
697
- # and <tt>$DEBUG</tt> to +true+;
698
- # these have no effect on \IRB output.
710
+ # Command-line option `-d` sets variables `$VERBOSE` and `$DEBUG` to `true`;
711
+ # these have no effect on IRB output.
699
712
  #
700
- # === Warnings
713
+ # ### Warnings
701
714
  #
702
- # Command-line option <tt>-w</tt> suppresses warnings.
715
+ # Command-line option `-w` suppresses warnings.
703
716
  #
704
- # Command-line option <tt>-W[_level_]<tt>
705
- # sets warning level; 0=silence, 1=medium, 2=verbose.
717
+ # Command-line option `-W[*level*]` sets warning level;
706
718
  #
707
- # == Other Features
719
+ # * 0=silence
720
+ # * 1=medium
721
+ # * 2=verbose
708
722
  #
709
- # === Load Modules
723
+ # ## Other Features
724
+ #
725
+ # ### Load Modules
710
726
  #
711
727
  # You can specify the names of modules that are to be required at startup.
712
728
  #
713
- # \Array <tt>conf.load_modules</tt> determines the modules (if any)
714
- # that are to be required during session startup.
715
- # The array is used only during session startup,
716
- # so the initial value is the only one that counts.
729
+ # Array `conf.load_modules` determines the modules (if any) that are to be
730
+ # required during session startup. The array is used only during session
731
+ # startup, so the initial value is the only one that counts.
717
732
  #
718
- # The default initial value is <tt>[]</tt> (load no modules):
733
+ # The default initial value is `[]` (load no modules):
719
734
  #
720
- # irb(main):001> conf.load_modules
721
- # => []
735
+ # irb(main):001> conf.load_modules
736
+ # => []
722
737
  #
723
738
  # You can set the default initial value via:
724
739
  #
725
- # - Command-line option <tt>-r</tt>
740
+ # * Command-line option `-r`
741
+ #
742
+ # $ irb -r csv -r json
743
+ # irb(main):001> conf.load_modules
744
+ # => ["csv", "json"]
726
745
  #
727
- # $ irb -r csv -r json
728
- # irb(main):001> conf.load_modules
729
- # => ["csv", "json"]
746
+ # * Hash entry `IRB.conf[:LOAD_MODULES] = *array*`:
730
747
  #
731
- # - \Hash entry <tt>IRB.conf[:LOAD_MODULES] = _array_</tt>:
748
+ # IRB.conf[:LOAD_MODULES] = %w[csv, json]
732
749
  #
733
- # IRB.conf[:LOAD_MODULES] = %w[csv, json]
734
750
  #
735
751
  # Note that the configuration file entry overrides the command-line options.
736
752
  #
737
- # === RI Documentation Directories
753
+ # ### RI Documentation Directories
738
754
  #
739
- # You can specify the paths to RI documentation directories
740
- # that are to be loaded (in addition to the default directories) at startup;
741
- # see details about RI by typing <tt>ri --help</tt>.
755
+ # You can specify the paths to RI documentation directories that are to be
756
+ # loaded (in addition to the default directories) at startup; see details about
757
+ # RI by typing `ri --help`.
742
758
  #
743
- # \Array <tt>conf.extra_doc_dirs</tt> determines the directories (if any)
744
- # that are to be loaded during session startup.
745
- # The array is used only during session startup,
759
+ # Array `conf.extra_doc_dirs` determines the directories (if any) that are to be
760
+ # loaded during session startup. The array is used only during session startup,
746
761
  # so the initial value is the only one that counts.
747
762
  #
748
- # The default initial value is <tt>[]</tt> (load no extra documentation):
763
+ # The default initial value is `[]` (load no extra documentation):
749
764
  #
750
- # irb(main):001> conf.extra_doc_dirs
751
- # => []
765
+ # irb(main):001> conf.extra_doc_dirs
766
+ # => []
752
767
  #
753
768
  # You can set the default initial value via:
754
769
  #
755
- # - Command-line option <tt>--extra_doc_dir</tt>
770
+ # * Command-line option `--extra_doc_dir`
756
771
  #
757
- # $ irb --extra-doc-dir your_doc_dir --extra-doc-dir my_doc_dir
758
- # irb(main):001> conf.extra_doc_dirs
759
- # => ["your_doc_dir", "my_doc_dir"]
772
+ # $ irb --extra-doc-dir your_doc_dir --extra-doc-dir my_doc_dir
773
+ # irb(main):001> conf.extra_doc_dirs
774
+ # => ["your_doc_dir", "my_doc_dir"]
760
775
  #
761
- # - \Hash entry <tt>IRB.conf[:EXTRA_DOC_DIRS] = _array_</tt>:
776
+ # * Hash entry `IRB.conf[:EXTRA_DOC_DIRS] = *array*`:
777
+ #
778
+ # IRB.conf[:EXTRA_DOC_DIRS] = %w[your_doc_dir my_doc_dir]
762
779
  #
763
- # IRB.conf[:EXTRA_DOC_DIRS] = %w[your_doc_dir my_doc_dir]
764
780
  #
765
781
  # Note that the configuration file entry overrides the command-line options.
766
782
  #
767
- # === \IRB Name
783
+ # ### IRB Name
768
784
  #
769
- # You can specify a name for \IRB.
785
+ # You can specify a name for IRB.
770
786
  #
771
- # The default initial value is <tt>'irb'</tt>:
787
+ # The default initial value is `'irb'`:
772
788
  #
773
- # irb(main):001> conf.irb_name
774
- # => "irb"
789
+ # irb(main):001> conf.irb_name
790
+ # => "irb"
775
791
  #
776
- # You can set the default initial value
777
- # via hash entry <tt>IRB.conf[:IRB_NAME] = _string_</tt>:
792
+ # You can set the default initial value via hash entry `IRB.conf[:IRB_NAME] =
793
+ # *string*`:
778
794
  #
779
- # IRB.conf[:IRB_NAME] = 'foo'
795
+ # IRB.conf[:IRB_NAME] = 'foo'
780
796
  #
781
- # === Application Name
797
+ # ### Application Name
782
798
  #
783
- # You can specify an application name for the \IRB session.
799
+ # You can specify an application name for the IRB session.
784
800
  #
785
- # The default initial value is <tt>'irb'</tt>:
801
+ # The default initial value is `'irb'`:
786
802
  #
787
- # irb(main):001> conf.ap_name
788
- # => "irb"
803
+ # irb(main):001> conf.ap_name
804
+ # => "irb"
789
805
  #
790
- # You can set the default initial value
791
- # via hash entry <tt>IRB.conf[:AP_NAME] = _string_</tt>:
806
+ # You can set the default initial value via hash entry `IRB.conf[:AP_NAME] =
807
+ # *string*`:
792
808
  #
793
- # IRB.conf[:AP_NAME] = 'my_ap_name'
809
+ # IRB.conf[:AP_NAME] = 'my_ap_name'
794
810
  #
795
- # === Configuration Monitor
811
+ # ### Configuration Monitor
796
812
  #
797
- # You can monitor changes to the configuration by assigning a proc
798
- # to <tt>IRB.conf[:IRB_RC]</tt> in the configuration file:
813
+ # You can monitor changes to the configuration by assigning a proc to
814
+ # `IRB.conf[:IRB_RC]` in the configuration file:
799
815
  #
800
- # IRB.conf[:IRB_RC] = proc {|conf| puts conf.class }
816
+ # IRB.conf[:IRB_RC] = proc {|conf| puts conf.class }
801
817
  #
802
- # Each time the configuration is changed,
803
- # that proc is called with argument +conf+:
818
+ # Each time the configuration is changed, that proc is called with argument
819
+ # `conf`:
804
820
  #
805
- # === Encodings
821
+ # ### Encodings
806
822
  #
807
- # Command-line option <tt>-E _ex_[:_in_]</tt>
808
- # sets initial external (ex) and internal (in) encodings.
823
+ # Command-line option `-E *ex*[:*in*]` sets initial external (ex) and internal
824
+ # (in) encodings.
809
825
  #
810
- # Command-line option <tt>-U</tt> sets both to UTF-8.
826
+ # Command-line option `-U` sets both to UTF-8.
811
827
  #
812
- # === Commands
828
+ # ### Commands
813
829
  #
814
830
  # Please use the `help` command to see the list of available commands.
815
831
  #
816
- # === IRB Sessions
832
+ # ### IRB Sessions
817
833
  #
818
834
  # IRB has a special feature, that allows you to manage many sessions at once.
819
835
  #
820
836
  # You can create new sessions with Irb.irb, and get a list of current sessions
821
- # with the +jobs+ command in the prompt.
837
+ # with the `jobs` command in the prompt.
822
838
  #
823
- # ==== Configuration
839
+ # #### Configuration
824
840
  #
825
841
  # The command line options, or IRB.conf, specify the default behavior of
826
842
  # Irb.irb.
@@ -828,32 +844,33 @@ require_relative "irb/pager"
828
844
  # On the other hand, each conf in IRB@Command-Line+Options is used to
829
845
  # individually configure IRB.irb.
830
846
  #
831
- # If a proc is set for <code>IRB.conf[:IRB_RC]</code>, its will be invoked after execution
847
+ # If a proc is set for `IRB.conf[:IRB_RC]`, its will be invoked after execution
832
848
  # of that proc with the context of the current session as its argument. Each
833
849
  # session can be configured using this mechanism.
834
850
  #
835
- # ==== Session variables
851
+ # #### Session variables
836
852
  #
837
853
  # There are a few variables in every Irb session that can come in handy:
838
854
  #
839
- # <code>_</code>::
840
- # The value command executed, as a local variable
841
- # <code>__</code>::
842
- # The history of evaluated commands. Available only if
843
- # <code>IRB.conf[:EVAL_HISTORY]</code> is not +nil+ (which is the default).
844
- # See also IRB::Context#eval_history= and IRB::History.
845
- # <code>__[line_no]</code>::
846
- # Returns the evaluation value at the given line number, +line_no+.
847
- # If +line_no+ is a negative, the return value +line_no+ many lines before
848
- # the most recent return value.
855
+ # `_`
856
+ # : The value command executed, as a local variable
857
+ # `__`
858
+ # : The history of evaluated commands. Available only if
859
+ # `IRB.conf[:EVAL_HISTORY]` is not `nil` (which is the default). See also
860
+ # IRB::Context#eval_history= and IRB::History.
861
+ # `__[line_no]`
862
+ # : Returns the evaluation value at the given line number, `line_no`. If
863
+ # `line_no` is a negative, the return value `line_no` many lines before the
864
+ # most recent return value.
865
+ #
849
866
  #
850
- # == Restrictions
867
+ # ## Restrictions
851
868
  #
852
- # Ruby code typed into \IRB behaves the same as Ruby code in a file, except that:
869
+ # Ruby code typed into IRB behaves the same as Ruby code in a file, except that:
853
870
  #
854
- # - Because \IRB evaluates input immediately after it is syntactically complete,
855
- # some results may be slightly different.
856
- # - Forking may not be well behaved.
871
+ # * Because IRB evaluates input immediately after it is syntactically
872
+ # complete, some results may be slightly different.
873
+ # * Forking may not be well behaved.
857
874
  #
858
875
  module IRB
859
876
 
@@ -862,14 +879,14 @@ module IRB
862
879
 
863
880
  # The current IRB::Context of the session, see IRB.conf
864
881
  #
865
- # irb
866
- # irb(main):001:0> IRB.CurrentContext.irb_name = "foo"
867
- # foo(main):002:0> IRB.conf[:MAIN_CONTEXT].irb_name #=> "foo"
868
- def IRB.CurrentContext
882
+ # irb
883
+ # irb(main):001:0> IRB.CurrentContext.irb_name = "foo"
884
+ # foo(main):002:0> IRB.conf[:MAIN_CONTEXT].irb_name #=> "foo"
885
+ def IRB.CurrentContext # :nodoc:
869
886
  IRB.conf[:MAIN_CONTEXT]
870
887
  end
871
888
 
872
- # Initializes IRB and creates a new Irb.irb object at the +TOPLEVEL_BINDING+
889
+ # Initializes IRB and creates a new Irb.irb object at the `TOPLEVEL_BINDING`
873
890
  def IRB.start(ap_path = nil)
874
891
  STDOUT.sync = true
875
892
  $0 = File::basename(ap_path, ".rb") if ap_path
@@ -885,22 +902,22 @@ module IRB
885
902
  end
886
903
 
887
904
  # Quits irb
888
- def IRB.irb_exit(*)
905
+ def IRB.irb_exit(*) # :nodoc:
889
906
  throw :IRB_EXIT, false
890
907
  end
891
908
 
892
909
  # Aborts then interrupts irb.
893
910
  #
894
- # Will raise an Abort exception, or the given +exception+.
895
- def IRB.irb_abort(irb, exception = Abort)
911
+ # Will raise an Abort exception, or the given `exception`.
912
+ def IRB.irb_abort(irb, exception = Abort) # :nodoc:
896
913
  irb.context.thread.raise exception, "abort then interrupt!"
897
914
  end
898
915
 
899
916
  class Irb
900
917
  # Note: instance and index assignment expressions could also be written like:
901
- # "foo.bar=(1)" and "foo.[]=(1, bar)", when expressed that way, the former
902
- # be parsed as :assign and echo will be suppressed, but the latter is
903
- # parsed as a :method_add_arg and the output won't be suppressed
918
+ # "foo.bar=(1)" and "foo.[]=(1, bar)", when expressed that way, the former be
919
+ # parsed as :assign and echo will be suppressed, but the latter is parsed as a
920
+ # :method_add_arg and the output won't be suppressed
904
921
 
905
922
  PROMPT_MAIN_TRUNCATE_LENGTH = 32
906
923
  PROMPT_MAIN_TRUNCATE_OMISSION = '...'
@@ -914,13 +931,14 @@ module IRB
914
931
  # Creates a new irb session
915
932
  def initialize(workspace = nil, input_method = nil)
916
933
  @context = Context.new(self, workspace, input_method)
917
- @context.workspace.load_commands_to_main
934
+ @context.workspace.load_helper_methods_to_main
918
935
  @signal_status = :IN_IRB
919
936
  @scanner = RubyLex.new
920
937
  @line_no = 1
921
938
  end
922
939
 
923
- # A hook point for `debug` command's breakpoint after :IRB_EXIT as well as its clean-up
940
+ # A hook point for `debug` command's breakpoint after :IRB_EXIT as well as its
941
+ # clean-up
924
942
  def debug_break
925
943
  # it means the debug integration has been activated
926
944
  if defined?(DEBUGGER__) && DEBUGGER__.respond_to?(:capture_frames_without_irb)
@@ -934,28 +952,36 @@ module IRB
934
952
  def debug_readline(binding)
935
953
  workspace = IRB::WorkSpace.new(binding)
936
954
  context.replace_workspace(workspace)
937
- context.workspace.load_commands_to_main
955
+ context.workspace.load_helper_methods_to_main
938
956
  @line_no += 1
939
957
 
940
958
  # When users run:
941
- # 1. Debugging commands, like `step 2`
942
- # 2. Any input that's not irb-command, like `foo = 123`
959
+ # 1. Debugging commands, like `step 2`
960
+ # 2. Any input that's not irb-command, like `foo = 123`
961
+ #
943
962
  #
944
- # Irb#eval_input will simply return the input, and we need to pass it to the debugger.
945
- input = if IRB.conf[:SAVE_HISTORY] && context.io.support_history_saving?
946
- # Previous IRB session's history has been saved when `Irb#run` is exited
947
- # We need to make sure the saved history is not saved again by resetting the counter
948
- context.io.reset_history_counter
963
+ # Irb#eval_input will simply return the input, and we need to pass it to the
964
+ # debugger.
965
+ input = nil
966
+ forced_exit = catch(:IRB_EXIT) do
967
+ if IRB.conf[:SAVE_HISTORY] && context.io.support_history_saving?
968
+ # Previous IRB session's history has been saved when `Irb#run` is exited We need
969
+ # to make sure the saved history is not saved again by resetting the counter
970
+ context.io.reset_history_counter
949
971
 
950
- begin
951
- eval_input
952
- ensure
953
- context.io.save_history
972
+ begin
973
+ input = eval_input
974
+ ensure
975
+ context.io.save_history
976
+ end
977
+ else
978
+ input = eval_input
954
979
  end
955
- else
956
- eval_input
980
+ false
957
981
  end
958
982
 
983
+ Kernel.exit if forced_exit
984
+
959
985
  if input&.include?("\n")
960
986
  @line_no += input.count("\n") - 1
961
987
  end
@@ -966,6 +992,7 @@ module IRB
966
992
  def run(conf = IRB.conf)
967
993
  in_nested_session = !!conf[:MAIN_CONTEXT]
968
994
  conf[:IRB_RC].call(context) if conf[:IRB_RC]
995
+ prev_context = conf[:MAIN_CONTEXT]
969
996
  conf[:MAIN_CONTEXT] = context
970
997
 
971
998
  save_history = !in_nested_session && conf[:SAVE_HISTORY] && context.io.support_history_saving?
@@ -988,6 +1015,9 @@ module IRB
988
1015
  eval_input
989
1016
  end
990
1017
  ensure
1018
+ # Do not restore to nil. It will cause IRB crash when used with threads.
1019
+ IRB.conf[:MAIN_CONTEXT] = prev_context if prev_context
1020
+
991
1021
  RubyVM.keep_script_lines = keep_script_lines_backup if defined?(RubyVM.keep_script_lines)
992
1022
  trap("SIGINT", prev_trap)
993
1023
  conf[:AT_EXIT].each{|hook| hook.call}
@@ -1004,12 +1034,13 @@ module IRB
1004
1034
  each_top_level_statement do |statement, line_no|
1005
1035
  signal_status(:IN_EVAL) do
1006
1036
  begin
1007
- # If the integration with debugger is activated, we return certain input if it should be dealt with by debugger
1037
+ # If the integration with debugger is activated, we return certain input if it
1038
+ # should be dealt with by debugger
1008
1039
  if @context.with_debugger && statement.should_be_handled_by_debugger?
1009
1040
  return statement.code
1010
1041
  end
1011
1042
 
1012
- @context.evaluate(statement.evaluable_code, line_no)
1043
+ @context.evaluate(statement, line_no)
1013
1044
 
1014
1045
  if @context.echo? && !statement.suppresses_echo?
1015
1046
  if statement.is_assignment?
@@ -1065,9 +1096,7 @@ module IRB
1065
1096
  end
1066
1097
 
1067
1098
  code << line
1068
-
1069
- # Accept any single-line input for symbol aliases or commands that transform args
1070
- return code if single_line_command?(code)
1099
+ return code if command?(code)
1071
1100
 
1072
1101
  tokens, opens, terminated = @scanner.check_code_state(code, local_variables: @context.local_variables)
1073
1102
  return code if terminated
@@ -1094,23 +1123,36 @@ module IRB
1094
1123
  end
1095
1124
 
1096
1125
  code.force_encoding(@context.io.encoding)
1097
- command_or_alias, arg = code.split(/\s/, 2)
1098
- # Transform a non-identifier alias (@, $) or keywords (next, break)
1099
- command_name = @context.command_aliases[command_or_alias.to_sym]
1100
- command = command_name || command_or_alias
1101
- command_class = ExtendCommandBundle.load_command(command)
1102
-
1103
- if command_class
1104
- Statement::Command.new(code, command, arg, command_class)
1126
+ if (command, arg = parse_command(code))
1127
+ command_class = Command.load_command(command)
1128
+ Statement::Command.new(code, command_class, arg)
1105
1129
  else
1106
1130
  is_assignment_expression = @scanner.assignment_expression?(code, local_variables: @context.local_variables)
1107
1131
  Statement::Expression.new(code, is_assignment_expression)
1108
1132
  end
1109
1133
  end
1110
1134
 
1111
- def single_line_command?(code)
1112
- command = code.split(/\s/, 2).first
1113
- @context.symbol_alias?(command) || @context.transform_args?(command)
1135
+ def parse_command(code)
1136
+ command_name, arg = code.strip.split(/\s+/, 2)
1137
+ return unless code.lines.size == 1 && command_name
1138
+
1139
+ arg ||= ''
1140
+ command = command_name.to_sym
1141
+ # Command aliases are always command. example: $, @
1142
+ if (alias_name = @context.command_aliases[command])
1143
+ return [alias_name, arg]
1144
+ end
1145
+
1146
+ # Check visibility
1147
+ public_method = !!Kernel.instance_method(:public_method).bind_call(@context.main, command) rescue false
1148
+ private_method = !public_method && !!Kernel.instance_method(:method).bind_call(@context.main, command) rescue false
1149
+ if Command.execute_as_command?(command, public_method: public_method, private_method: private_method)
1150
+ [command, arg]
1151
+ end
1152
+ end
1153
+
1154
+ def command?(code)
1155
+ !!parse_command(code)
1114
1156
  end
1115
1157
 
1116
1158
  def configure_io
@@ -1128,8 +1170,7 @@ module IRB
1128
1170
  false
1129
1171
  end
1130
1172
  else
1131
- # Accept any single-line input for symbol aliases or commands that transform args
1132
- next true if single_line_command?(code)
1173
+ next true if command?(code)
1133
1174
 
1134
1175
  _tokens, _opens, terminated = @scanner.check_code_state(code, local_variables: @context.local_variables)
1135
1176
  terminated
@@ -1143,7 +1184,8 @@ module IRB
1143
1184
  tokens_until_line = []
1144
1185
  line_results.map.with_index do |(line_tokens, _prev_opens, next_opens, _min_depth), line_num_offset|
1145
1186
  line_tokens.each do |token, _s|
1146
- # Avoid appending duplicated token. Tokens that include "\n" like multiline tstring_content can exist in multiple lines.
1187
+ # Avoid appending duplicated token. Tokens that include "n" like multiline
1188
+ # tstring_content can exist in multiple lines.
1147
1189
  tokens_until_line << token if token != tokens_until_line.last
1148
1190
  end
1149
1191
  continue = @scanner.should_continue?(tokens_until_line)
@@ -1200,20 +1242,33 @@ module IRB
1200
1242
  irb_bug = true
1201
1243
  else
1202
1244
  irb_bug = false
1245
+ # To support backtrace filtering while utilizing Exception#full_message, we need to clone
1246
+ # the exception to avoid modifying the original exception's backtrace.
1247
+ exc = exc.clone
1248
+ filtered_backtrace = exc.backtrace.map { |l| @context.workspace.filter_backtrace(l) }.compact
1249
+ backtrace_filter = IRB.conf[:BACKTRACE_FILTER]
1250
+
1251
+ if backtrace_filter
1252
+ if backtrace_filter.respond_to?(:call)
1253
+ filtered_backtrace = backtrace_filter.call(filtered_backtrace)
1254
+ else
1255
+ warn "IRB.conf[:BACKTRACE_FILTER] #{backtrace_filter} should respond to `call` method"
1256
+ end
1257
+ end
1258
+
1259
+ exc.set_backtrace(filtered_backtrace)
1203
1260
  end
1204
1261
 
1205
- if RUBY_VERSION < '3.0.0'
1206
- if STDOUT.tty?
1207
- message = exc.full_message(order: :bottom)
1208
- order = :bottom
1209
- else
1210
- message = exc.full_message(order: :top)
1211
- order = :top
1262
+ highlight = Color.colorable?
1263
+
1264
+ order =
1265
+ if RUBY_VERSION < '3.0.0'
1266
+ STDOUT.tty? ? :bottom : :top
1267
+ else # '3.0.0' <= RUBY_VERSION
1268
+ :top
1212
1269
  end
1213
- else # '3.0.0' <= RUBY_VERSION
1214
- message = exc.full_message(order: :top)
1215
- order = :top
1216
- end
1270
+
1271
+ message = exc.full_message(order: order, highlight: highlight)
1217
1272
  message = convert_invalid_byte_sequence(message, exc.message.encoding)
1218
1273
  message = encode_with_invalid_byte_sequence(message, IRB.conf[:LC_MESSAGES].encoding) unless message.encoding.to_s.casecmp?(IRB.conf[:LC_MESSAGES].encoding.to_s)
1219
1274
  message = message.gsub(/((?:^\t.+$\n)+)/) { |m|
@@ -1224,7 +1279,6 @@ module IRB
1224
1279
  lines = m.split("\n").reverse
1225
1280
  end
1226
1281
  unless irb_bug
1227
- lines = lines.map { |l| @context.workspace.filter_backtrace(l) }.compact
1228
1282
  if lines.size > @context.back_trace_limit
1229
1283
  omit = lines.size - @context.back_trace_limit
1230
1284
  lines = lines[0..(@context.back_trace_limit - 1)]
@@ -1247,11 +1301,10 @@ module IRB
1247
1301
  end
1248
1302
  end
1249
1303
 
1250
- # Evaluates the given block using the given +path+ as the Context#irb_path
1251
- # and +name+ as the Context#irb_name.
1304
+ # Evaluates the given block using the given `path` as the Context#irb_path and
1305
+ # `name` as the Context#irb_name.
1252
1306
  #
1253
- # Used by the irb command +source+, see IRB@IRB+Sessions for more
1254
- # information.
1307
+ # Used by the irb command `source`, see IRB@IRB+Sessions for more information.
1255
1308
  def suspend_name(path = nil, name = nil)
1256
1309
  @context.irb_path, back_path = path, @context.irb_path if path
1257
1310
  @context.irb_name, back_name = name, @context.irb_name if name
@@ -1263,11 +1316,10 @@ module IRB
1263
1316
  end
1264
1317
  end
1265
1318
 
1266
- # Evaluates the given block using the given +workspace+ as the
1319
+ # Evaluates the given block using the given `workspace` as the
1267
1320
  # Context#workspace.
1268
1321
  #
1269
- # Used by the irb command +irb_load+, see IRB@IRB+Sessions for more
1270
- # information.
1322
+ # Used by the irb command `irb_load`, see IRB@IRB+Sessions for more information.
1271
1323
  def suspend_workspace(workspace)
1272
1324
  current_workspace = @context.workspace
1273
1325
  @context.replace_workspace(workspace)
@@ -1276,11 +1328,10 @@ module IRB
1276
1328
  @context.replace_workspace current_workspace
1277
1329
  end
1278
1330
 
1279
- # Evaluates the given block using the given +input_method+ as the
1280
- # Context#io.
1331
+ # Evaluates the given block using the given `input_method` as the Context#io.
1281
1332
  #
1282
- # Used by the irb commands +source+ and +irb_load+, see IRB@IRB+Sessions
1283
- # for more information.
1333
+ # Used by the irb commands `source` and `irb_load`, see IRB@IRB+Sessions for
1334
+ # more information.
1284
1335
  def suspend_input_method(input_method)
1285
1336
  back_io = @context.io
1286
1337
  @context.instance_eval{@io = input_method}
@@ -1313,7 +1364,7 @@ module IRB
1313
1364
  end
1314
1365
  end
1315
1366
 
1316
- # Evaluates the given block using the given +status+.
1367
+ # Evaluates the given block using the given `status`.
1317
1368
  def signal_status(status)
1318
1369
  return yield if @signal_status == :IN_LOAD
1319
1370
 
@@ -1363,8 +1414,8 @@ module IRB
1363
1414
  Pager.page_content(format(@context.return_format, str), retain_content: true)
1364
1415
  end
1365
1416
 
1366
- # Outputs the local variables to this current session, including
1367
- # #signal_status and #context, using IRB::Locale.
1417
+ # Outputs the local variables to this current session, including #signal_status
1418
+ # and #context, using IRB::Locale.
1368
1419
  def inspect
1369
1420
  ary = []
1370
1421
  for iv in instance_variables
@@ -1422,7 +1473,7 @@ module IRB
1422
1473
  end
1423
1474
 
1424
1475
  def format_prompt(format, ltype, indent, line_no) # :nodoc:
1425
- format.gsub(/%([0-9]+)?([a-zA-Z])/) do
1476
+ format.gsub(/%([0-9]+)?([a-zA-Z%])/) do
1426
1477
  case $2
1427
1478
  when "N"
1428
1479
  @context.irb_name
@@ -1455,7 +1506,7 @@ module IRB
1455
1506
  line_no.to_s
1456
1507
  end
1457
1508
  when "%"
1458
- "%"
1509
+ "%" unless $1
1459
1510
  end
1460
1511
  end
1461
1512
  end
@@ -1463,12 +1514,11 @@ module IRB
1463
1514
  end
1464
1515
 
1465
1516
  class Binding
1466
- # Opens an IRB session where +binding.irb+ is called which allows for
1467
- # interactive debugging. You can call any methods or variables available in
1468
- # the current scope, and mutate state if you need to.
1469
- #
1517
+ # Opens an IRB session where `binding.irb` is called which allows for
1518
+ # interactive debugging. You can call any methods or variables available in the
1519
+ # current scope, and mutate state if you need to.
1470
1520
  #
1471
- # Given a Ruby file called +potato.rb+ containing the following code:
1521
+ # Given a Ruby file called `potato.rb` containing the following code:
1472
1522
  #
1473
1523
  # class Potato
1474
1524
  # def initialize
@@ -1480,8 +1530,8 @@ class Binding
1480
1530
  #
1481
1531
  # Potato.new
1482
1532
  #
1483
- # Running <code>ruby potato.rb</code> will open an IRB session where
1484
- # +binding.irb+ is called, and you will see the following:
1533
+ # Running `ruby potato.rb` will open an IRB session where `binding.irb` is
1534
+ # called, and you will see the following:
1485
1535
  #
1486
1536
  # $ ruby potato.rb
1487
1537
  #
@@ -1511,8 +1561,8 @@ class Binding
1511
1561
  # irb(#<Potato:0x00007feea1916670>):004:0> @cooked = true
1512
1562
  # => true
1513
1563
  #
1514
- # You can exit the IRB session with the +exit+ command. Note that exiting will
1515
- # resume execution where +binding.irb+ had paused it, as you can see from the
1564
+ # You can exit the IRB session with the `exit` command. Note that exiting will
1565
+ # resume execution where `binding.irb` had paused it, as you can see from the
1516
1566
  # output printed to standard output in this example:
1517
1567
  #
1518
1568
  # irb(#<Potato:0x00007feea1916670>):005:0> exit
@@ -1535,13 +1585,14 @@ class Binding
1535
1585
  # If we're already in a debugger session, set the workspace and irb_path for the original IRB instance
1536
1586
  debugger_irb.context.replace_workspace(workspace)
1537
1587
  debugger_irb.context.irb_path = irb_path
1538
- # If we've started a debugger session and hit another binding.irb, we don't want to start an IRB session
1539
- # instead, we want to resume the irb:rdbg session.
1588
+ # If we've started a debugger session and hit another binding.irb, we don't want
1589
+ # to start an IRB session instead, we want to resume the irb:rdbg session.
1540
1590
  IRB::Debug.setup(debugger_irb)
1541
1591
  IRB::Debug.insert_debug_break
1542
1592
  debugger_irb.debug_break
1543
1593
  else
1544
- # If we're not in a debugger session, create a new IRB instance with the current workspace
1594
+ # If we're not in a debugger session, create a new IRB instance with the current
1595
+ # workspace
1545
1596
  binding_irb = IRB::Irb.new(workspace)
1546
1597
  binding_irb.context.irb_path = irb_path
1547
1598
  binding_irb.run(IRB.conf)