irb 1.14.3 → 1.15.3
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/.rdoc_options +5 -0
- data/CONTRIBUTING.md +52 -0
- data/EXTEND_IRB.md +3 -0
- data/Gemfile +4 -0
- data/README.md +13 -310
- data/doc/COMMAND_LINE_OPTIONS.md +69 -0
- data/doc/COMPARED_WITH_PRY.md +22 -0
- data/doc/Configurations.md +275 -0
- data/doc/EXTEND_IRB.md +122 -0
- data/doc/Index.md +705 -0
- data/lib/irb/color.rb +1 -0
- data/lib/irb/color_printer.rb +10 -9
- data/lib/irb/command/copy.rb +73 -0
- data/lib/irb/command/history.rb +1 -1
- data/lib/irb/completion.rb +31 -36
- data/lib/irb/context.rb +95 -60
- data/lib/irb/debug.rb +2 -2
- data/lib/irb/default_commands.rb +4 -1
- data/lib/irb/easter-egg.rb +3 -1
- data/lib/irb/history.rb +4 -0
- data/lib/irb/init.rb +3 -1
- data/lib/irb/input-method.rb +17 -11
- data/lib/irb/inspector.rb +12 -7
- data/lib/irb/pager.rb +119 -6
- data/lib/irb/ruby-lex.rb +25 -0
- data/lib/irb/source_finder.rb +1 -1
- data/lib/irb/statement.rb +21 -0
- data/lib/irb/version.rb +2 -2
- data/lib/irb.rb +73 -910
- data/man/irb.1 +2 -0
- metadata +27 -12
- data/.document +0 -8
- data/Rakefile +0 -52
- data/bin/console +0 -6
- data/bin/setup +0 -6
- data/irb.gemspec +0 -46
|
@@ -0,0 +1,275 @@
|
|
|
1
|
+
# Configure IRB
|
|
2
|
+
|
|
3
|
+
## Configuration Sources
|
|
4
|
+
|
|
5
|
+
IRB configurations can be set through multiple sources, each with its own precedence:
|
|
6
|
+
|
|
7
|
+
1. **Command-Line Options**: When some options are specified when starting IRB, they can override default settings.
|
|
8
|
+
2. **Configuration File**: If present, IRB reads a configuration file containing Ruby code to set configurations.
|
|
9
|
+
3. **Environment Variables**: Certain environment variables influence IRB's behavior.
|
|
10
|
+
4. **Hash `IRB.conf`**: This hash holds the current configuration settings, which can be modified during a session.
|
|
11
|
+
|
|
12
|
+
### Configuration File Path Resolution
|
|
13
|
+
|
|
14
|
+
IRB searches for a configuration file in the following order:
|
|
15
|
+
|
|
16
|
+
1. `$IRBRC`
|
|
17
|
+
2. `$XDG_CONFIG_HOME/irb/irbrc`
|
|
18
|
+
3. `$HOME/.irbrc`
|
|
19
|
+
4. `$HOME/.config/irb/irbrc`
|
|
20
|
+
5. `.irbrc` in the current directory
|
|
21
|
+
6. `irb.rc` in the current directory
|
|
22
|
+
7. `_irbrc` in the current directory
|
|
23
|
+
8. `$irbrc` in the current directory
|
|
24
|
+
|
|
25
|
+
If the `-f` command-line option is used, no configuration file is loaded.
|
|
26
|
+
|
|
27
|
+
Method `conf.rc?` returns `true` if a configuration file was read, `false` otherwise. Hash entry `IRB.conf[:RC]` also contains that value.
|
|
28
|
+
|
|
29
|
+
## Environment Variables
|
|
30
|
+
|
|
31
|
+
- `NO_COLOR`: Disables IRB's colorization.
|
|
32
|
+
- `IRB_USE_AUTOCOMPLETE`: Setting to `false` disables autocompletion.
|
|
33
|
+
- `IRB_COMPLETOR`: Configures auto-completion behavior (`regexp` or `type`).
|
|
34
|
+
- `IRB_COPY_COMMAND`: Overrides the default program used to interface with the system clipboard.
|
|
35
|
+
- `VISUAL` / `EDITOR`: Specifies the editor for the `edit` command.
|
|
36
|
+
- `IRBRC`: Specifies the rc-file for configuration.
|
|
37
|
+
- `XDG_CONFIG_HOME`: Used to locate the rc-file if `IRBRC` is unset.
|
|
38
|
+
- `RI_PAGER` / `PAGER`: Specifies the pager for documentation.
|
|
39
|
+
- `IRB_LANG`, `LC_MESSAGES`, `LC_ALL`, `LANG`: Determines the locale.
|
|
40
|
+
|
|
41
|
+
## Hash `IRB.conf`
|
|
42
|
+
|
|
43
|
+
The initial entries in hash `IRB.conf` are determined by:
|
|
44
|
+
|
|
45
|
+
- Default values.
|
|
46
|
+
- Command-line options, which may override defaults.
|
|
47
|
+
- Direct assignments in the configuration file.
|
|
48
|
+
|
|
49
|
+
You can see the hash by typing `IRB.conf`. Below are the primary entries:
|
|
50
|
+
|
|
51
|
+
- `:AP_NAME`: IRB {application name}[rdoc-ref:IRB@Application+Name];
|
|
52
|
+
initial value: `'irb'`.
|
|
53
|
+
- `:AT_EXIT`: Array of hooks to call
|
|
54
|
+
{at exit}[rdoc-ref:IRB@IRB];
|
|
55
|
+
initial value: `[]`.
|
|
56
|
+
- `:AUTO_INDENT`: Whether {automatic indentation}[rdoc-ref:IRB@Automatic+Indentation]
|
|
57
|
+
is enabled; initial value: `true`.
|
|
58
|
+
- `:BACK_TRACE_LIMIT`: Sets the {back trace limit}[rdoc-ref:IRB@Tracer];
|
|
59
|
+
initial value: `16`.
|
|
60
|
+
- `:COMMAND_ALIASES`: Defines input {command aliases}[rdoc-ref:IRB@Command+Aliases];
|
|
61
|
+
initial value:
|
|
62
|
+
|
|
63
|
+
{
|
|
64
|
+
"$": :show_source,
|
|
65
|
+
"@": :whereami,
|
|
66
|
+
}
|
|
67
|
+
|
|
68
|
+
- `:CONTEXT_MODE`: Sets the {context mode}[rdoc-ref:IRB@Context+Mode],
|
|
69
|
+
the type of binding to be used when evaluating statements;
|
|
70
|
+
initial value: `4`.
|
|
71
|
+
- `:ECHO`: Whether to print ({echo}[rdoc-ref:IRB@Return-Value+Printing+-28Echoing-29])
|
|
72
|
+
return values;
|
|
73
|
+
initial value: `nil`, which would set `conf.echo` to `true`.
|
|
74
|
+
- `:ECHO_ON_ASSIGNMENT`: Whether to print ({echo}[rdoc-ref:IRB@Return-Value+Printing+-28Echoing-29])
|
|
75
|
+
return values on assignment;
|
|
76
|
+
initial value: `nil`, which would set `conf.echo_on_assignment` to `:truncate`.
|
|
77
|
+
- `:EVAL_HISTORY`: How much {evaluation history}[rdoc-ref:IRB@Evaluation+History]
|
|
78
|
+
is to be stored; initial value: `nil`.
|
|
79
|
+
- `:EXTRA_DOC_DIRS`: Array of
|
|
80
|
+
{RI documentation directories}[rdoc-ref:IRB@RI+Documentation+Directories]
|
|
81
|
+
to be parsed for the documentation dialog;
|
|
82
|
+
initial value: `[]`.
|
|
83
|
+
- `:IGNORE_EOF`: Whether to ignore {end-of-file}[rdoc-ref:IRB@End-of-File];
|
|
84
|
+
initial value: `false`.
|
|
85
|
+
- `:IGNORE_SIGINT`: Whether to ignore {SIGINT}[rdoc-ref:IRB@SIGINT];
|
|
86
|
+
initial value: `true`.
|
|
87
|
+
- `:INSPECT_MODE`: Whether to use method `inspect` for printing
|
|
88
|
+
({echoing}[rdoc-ref:IRB@Return-Value+Printing+-28Echoing-29]) return values;
|
|
89
|
+
initial value: `true`.
|
|
90
|
+
- `:IRB_LIB_PATH`: The path to the {IRB library directory}[rdoc-ref:IRB@IRB+Library+Directory]; initial value:
|
|
91
|
+
|
|
92
|
+
- `"<i>RUBY_DIR</i>/lib/ruby/gems/<i>RUBY_VER_NUM</i>/gems/irb-<i>IRB_VER_NUM</i>/lib/irb"`,
|
|
93
|
+
|
|
94
|
+
where:
|
|
95
|
+
|
|
96
|
+
- <i>RUBY_DIR</i> is the Ruby installation dirpath.
|
|
97
|
+
- <i>RUBY_VER_NUM</i> is the Ruby version number.
|
|
98
|
+
- <i>IRB_VER_NUM</i> is the IRB version number.
|
|
99
|
+
|
|
100
|
+
- `:IRB_NAME`: {IRB name}[rdoc-ref:IRB@IRB+Name];
|
|
101
|
+
initial value: `'irb'`.
|
|
102
|
+
- `:IRB_RC`: {Configuration monitor}[rdoc-ref:IRB@Configuration+Monitor];
|
|
103
|
+
initial value: `nil`.
|
|
104
|
+
- `:LC_MESSAGES`: {Locale}[rdoc-ref:IRB@Locale];
|
|
105
|
+
initial value: IRB::Locale object.
|
|
106
|
+
- `:LOAD_MODULES`: deprecated.
|
|
107
|
+
- `:MAIN_CONTEXT`: The {context}[rdoc-ref:IRB@Session+Context] for the main IRB session;
|
|
108
|
+
initial value: IRB::Context object.
|
|
109
|
+
- `:MEASURE`: Whether to
|
|
110
|
+
{measure performance}[rdoc-ref:IRB@Performance+Measurement];
|
|
111
|
+
initial value: `false`.
|
|
112
|
+
- `:MEASURE_CALLBACKS`: Callback methods for
|
|
113
|
+
{performance measurement}[rdoc-ref:IRB@Performance+Measurement];
|
|
114
|
+
initial value: `[]`.
|
|
115
|
+
- `:MEASURE_PROC`: Procs for
|
|
116
|
+
{performance measurement}[rdoc-ref:IRB@Performance+Measurement];
|
|
117
|
+
initial value:
|
|
118
|
+
|
|
119
|
+
{
|
|
120
|
+
:TIME=>#<Proc:0x0000556e271c6598 /var/lib/gems/3.0.0/gems/irb-1.8.3/lib/irb/init.rb:106>,
|
|
121
|
+
:STACKPROF=>#<Proc:0x0000556e271c6548 /var/lib/gems/3.0.0/gems/irb-1.8.3/lib/irb/init.rb:116>
|
|
122
|
+
}
|
|
123
|
+
|
|
124
|
+
- `:PROMPT`: Hash of {defined prompts}[rdoc-ref:IRB@Prompt+and+Return+Formats];
|
|
125
|
+
initial value:
|
|
126
|
+
|
|
127
|
+
{
|
|
128
|
+
:NULL=>{:PROMPT_I=>nil, :PROMPT_S=>nil, :PROMPT_C=>nil, :RETURN=>"%s\n"},
|
|
129
|
+
:DEFAULT=>{:PROMPT_I=>"%N(%m):%03n> ", :PROMPT_S=>"%N(%m):%03n%l ", :PROMPT_C=>"%N(%m):%03n* ", :RETURN=>"=> %s\n"},
|
|
130
|
+
:CLASSIC=>{:PROMPT_I=>"%N(%m):%03n:%i> ", :PROMPT_S=>"%N(%m):%03n:%i%l ", :PROMPT_C=>"%N(%m):%03n:%i* ", :RETURN=>"%s\n"},
|
|
131
|
+
:SIMPLE=>{:PROMPT_I=>">> ", :PROMPT_S=>"%l> ", :PROMPT_C=>"?> ", :RETURN=>"=> %s\n"},
|
|
132
|
+
:INF_RUBY=>{:PROMPT_I=>"%N(%m):%03n> ", :PROMPT_S=>nil, :PROMPT_C=>nil, :RETURN=>"%s\n", :AUTO_INDENT=>true},
|
|
133
|
+
:XMP=>{:PROMPT_I=>nil, :PROMPT_S=>nil, :PROMPT_C=>nil, :RETURN=>" ==>%s\n"}
|
|
134
|
+
}
|
|
135
|
+
|
|
136
|
+
- `:PROMPT_MODE`: Name of {current prompt}[rdoc-ref:IRB@Prompt+and+Return+Formats];
|
|
137
|
+
initial value: `:DEFAULT`.
|
|
138
|
+
- `:RC`: Whether a {configuration file}[rdoc-ref:IRB@Configuration+File]
|
|
139
|
+
was found and interpreted;
|
|
140
|
+
initial value: `true` if a configuration file was found, `false` otherwise.
|
|
141
|
+
- `:SAVE_HISTORY`: Number of commands to save in
|
|
142
|
+
{input command history}[rdoc-ref:IRB@Input+Command+History];
|
|
143
|
+
initial value: `1000`.
|
|
144
|
+
- `:SINGLE_IRB`: Whether command-line option `--single-irb` was given;
|
|
145
|
+
initial value: `true` if the option was given, `false` otherwise.
|
|
146
|
+
See {Single-IRB Mode}[rdoc-ref:IRB@Single-IRB+Mode].
|
|
147
|
+
- `:USE_AUTOCOMPLETE`: Whether to use
|
|
148
|
+
{automatic completion}[rdoc-ref:IRB@Automatic+Completion];
|
|
149
|
+
initial value: `true`.
|
|
150
|
+
- `:USE_COLORIZE`: Whether to use
|
|
151
|
+
{color highlighting}[rdoc-ref:IRB@Color+Highlighting];
|
|
152
|
+
initial value: `true`.
|
|
153
|
+
- `:USE_LOADER`: Whether to use the
|
|
154
|
+
{IRB loader}[rdoc-ref:IRB@IRB+Loader] for `require` and `load`;
|
|
155
|
+
initial value: `false`.
|
|
156
|
+
- `:USE_TRACER`: Whether to use the
|
|
157
|
+
{IRB tracer}[rdoc-ref:IRB@Tracer];
|
|
158
|
+
initial value: `false`.
|
|
159
|
+
- `:USE_PAGER`: Controls whether pager is enabled.
|
|
160
|
+
initial value: `true`.
|
|
161
|
+
- `:VERBOSE`: Whether to print {verbose output}[rdoc-ref:IRB@Verbosity];
|
|
162
|
+
initial value: `nil`.
|
|
163
|
+
- `:__MAIN__`: The main IRB object;
|
|
164
|
+
initial value: `main`.
|
|
165
|
+
|
|
166
|
+
## Notes on Initialization Precedence
|
|
167
|
+
|
|
168
|
+
- Any conflict between an entry in hash `IRB.conf` and a command-line option is resolved in favor of the hash entry.
|
|
169
|
+
- Hash `IRB.conf` affects the context only once, when the configuration file is interpreted; any subsequent changes to it do not affect the context and are therefore essentially meaningless.
|
|
170
|
+
|
|
171
|
+
## Load Modules
|
|
172
|
+
|
|
173
|
+
You can specify the names of modules that are to be required at startup.
|
|
174
|
+
|
|
175
|
+
Array `conf.load_modules` determines the modules (if any) that are to be required during session startup. The array is used only during session startup, so the initial value is the only one that counts.
|
|
176
|
+
|
|
177
|
+
The default initial value is `[]` (load no modules):
|
|
178
|
+
|
|
179
|
+
```console
|
|
180
|
+
irb(main):001> conf.load_modules
|
|
181
|
+
=> []
|
|
182
|
+
```
|
|
183
|
+
|
|
184
|
+
You can set the default initial value via:
|
|
185
|
+
|
|
186
|
+
- Command-line option `-r`
|
|
187
|
+
|
|
188
|
+
```console
|
|
189
|
+
$ irb -r csv -r json
|
|
190
|
+
irb(main):001> conf.load_modules
|
|
191
|
+
=> ["csv", "json"]
|
|
192
|
+
```
|
|
193
|
+
|
|
194
|
+
- Hash entry `IRB.conf[:LOAD_MODULES] = *array*`:
|
|
195
|
+
|
|
196
|
+
```ruby
|
|
197
|
+
IRB.conf[:LOAD_MODULES] = %w[csv json]
|
|
198
|
+
```
|
|
199
|
+
|
|
200
|
+
Note that the configuration file entry overrides the command-line options.
|
|
201
|
+
|
|
202
|
+
## RI Documentation Directories
|
|
203
|
+
|
|
204
|
+
You can specify the paths to RI documentation directories that are to be loaded (in addition to the default directories) at startup; see details about RI by typing `ri --help`.
|
|
205
|
+
|
|
206
|
+
Array `conf.extra_doc_dirs` determines the directories (if any) that are to be loaded during session startup. The array is used only during session startup, so the initial value is the only one that counts.
|
|
207
|
+
|
|
208
|
+
The default initial value is `[]` (load no extra documentation):
|
|
209
|
+
|
|
210
|
+
```console
|
|
211
|
+
irb(main):001> conf.extra_doc_dirs
|
|
212
|
+
=> []
|
|
213
|
+
```
|
|
214
|
+
|
|
215
|
+
You can set the default initial value via:
|
|
216
|
+
|
|
217
|
+
- Command-line option `--extra_doc_dir`
|
|
218
|
+
|
|
219
|
+
```console
|
|
220
|
+
$ irb --extra-doc-dir your_doc_dir --extra-doc-dir my_doc_dir
|
|
221
|
+
irb(main):001> conf.extra_doc_dirs
|
|
222
|
+
=> ["your_doc_dir", "my_doc_dir"]
|
|
223
|
+
```
|
|
224
|
+
|
|
225
|
+
- Hash entry `IRB.conf[:EXTRA_DOC_DIRS] = *array*`:
|
|
226
|
+
|
|
227
|
+
```ruby
|
|
228
|
+
IRB.conf[:EXTRA_DOC_DIRS] = %w[your_doc_dir my_doc_dir]
|
|
229
|
+
```
|
|
230
|
+
|
|
231
|
+
Note that the configuration file entry overrides the command-line options.
|
|
232
|
+
|
|
233
|
+
## IRB Name
|
|
234
|
+
|
|
235
|
+
You can specify a name for IRB.
|
|
236
|
+
|
|
237
|
+
The default initial value is `'irb'`:
|
|
238
|
+
|
|
239
|
+
```console
|
|
240
|
+
irb(main):001> conf.irb_name
|
|
241
|
+
=> "irb"
|
|
242
|
+
```
|
|
243
|
+
|
|
244
|
+
You can set the default initial value via hash entry `IRB.conf[:IRB_NAME] = *string*`:
|
|
245
|
+
|
|
246
|
+
```ruby
|
|
247
|
+
IRB.conf[:IRB_NAME] = 'foo'
|
|
248
|
+
```
|
|
249
|
+
|
|
250
|
+
## Application Name
|
|
251
|
+
|
|
252
|
+
You can specify an application name for the IRB session.
|
|
253
|
+
|
|
254
|
+
The default initial value is `'irb'`:
|
|
255
|
+
|
|
256
|
+
```console
|
|
257
|
+
irb(main):001> conf.ap_name
|
|
258
|
+
=> "irb"
|
|
259
|
+
```
|
|
260
|
+
|
|
261
|
+
You can set the default initial value via hash entry `IRB.conf[:AP_NAME] = *string*`:
|
|
262
|
+
|
|
263
|
+
```ruby
|
|
264
|
+
IRB.conf[:AP_NAME] = 'my_ap_name'
|
|
265
|
+
```
|
|
266
|
+
|
|
267
|
+
## Configuration Monitor
|
|
268
|
+
|
|
269
|
+
You can monitor changes to the configuration by assigning a proc to `IRB.conf[:IRB_RC]` in the configuration file:
|
|
270
|
+
|
|
271
|
+
```console
|
|
272
|
+
IRB.conf[:IRB_RC] = proc {|conf| puts conf.class }
|
|
273
|
+
```
|
|
274
|
+
|
|
275
|
+
Each time the configuration is changed, that proc is called with argument `conf`:
|
data/doc/EXTEND_IRB.md
ADDED
|
@@ -0,0 +1,122 @@
|
|
|
1
|
+
# Extend IRB
|
|
2
|
+
|
|
3
|
+
From v1.13.0, IRB provides official APIs to extend its functionality. This feature allows libraries to
|
|
4
|
+
customize and enhance their users' IRB sessions by adding new commands and helper methods tailored for
|
|
5
|
+
the libraries.
|
|
6
|
+
|
|
7
|
+
## Helper Methods vs. Commands
|
|
8
|
+
|
|
9
|
+
- Use a helper method if the operation is meant to return a Ruby object that interacts with the application.
|
|
10
|
+
- For example, an `admin_user` helper method that returns `User.where(admin: true).first`, which can then be used like `login_as(admin_user)`.
|
|
11
|
+
- Use a command if the operation fits one of the following:
|
|
12
|
+
- A utility operation that performs non-Ruby related tasks, such as IRB's `edit` command.
|
|
13
|
+
- Displays information, like the `show_source` command.
|
|
14
|
+
- If the operation requires non-Ruby syntax arguments, like `ls -g pattern`.
|
|
15
|
+
|
|
16
|
+
If you don't know what to pick, go with commands first. Commands are generally safer as they can handle a wider variety of inputs and use cases.
|
|
17
|
+
|
|
18
|
+
## Commands
|
|
19
|
+
|
|
20
|
+
Commands are designed to complete certain tasks or display information for the user, similar to shell commands.
|
|
21
|
+
Therefore, they are designed to accept a variety of inputs, including those that are not valid Ruby code, such
|
|
22
|
+
as `my_cmd Foo#bar` or `my_cmd --flag foo`.
|
|
23
|
+
|
|
24
|
+
### Example
|
|
25
|
+
|
|
26
|
+
```rb
|
|
27
|
+
require "irb/command"
|
|
28
|
+
|
|
29
|
+
class Greet < IRB::Command::Base
|
|
30
|
+
category "Greeting"
|
|
31
|
+
description "Greets the user"
|
|
32
|
+
help_message <<~HELP
|
|
33
|
+
Greets the user with the given name.
|
|
34
|
+
|
|
35
|
+
Usage: greet <name>
|
|
36
|
+
HELP
|
|
37
|
+
|
|
38
|
+
# Any input after the command name will be passed as a single string.
|
|
39
|
+
# If nothing is added after the command, an empty string will be passed.
|
|
40
|
+
def execute(arg)
|
|
41
|
+
puts "Hello! #{arg}"
|
|
42
|
+
end
|
|
43
|
+
end
|
|
44
|
+
|
|
45
|
+
IRB::Command.register(:greet, Greet)
|
|
46
|
+
```
|
|
47
|
+
|
|
48
|
+
As long as the above code is loaded before the IRB session is started, such as in a loaded library or a user's `.irbrc` file, `greet` will be accessible to the user.
|
|
49
|
+
|
|
50
|
+
```txt
|
|
51
|
+
irb(main):001> greet
|
|
52
|
+
Hello!
|
|
53
|
+
=> nil
|
|
54
|
+
irb(main):002> greet Stan
|
|
55
|
+
Hello! Stan
|
|
56
|
+
=> nil
|
|
57
|
+
```
|
|
58
|
+
|
|
59
|
+
And because the `Greet` command introduces a new category, `Greeting`, a new help message category will be created:
|
|
60
|
+
|
|
61
|
+
```txt
|
|
62
|
+
Help
|
|
63
|
+
help List all available commands. Use `help <command>` to get information about a specific command.
|
|
64
|
+
|
|
65
|
+
Greeting
|
|
66
|
+
greet Greets the user
|
|
67
|
+
|
|
68
|
+
IRB
|
|
69
|
+
context Displays current configuration.
|
|
70
|
+
...
|
|
71
|
+
```
|
|
72
|
+
|
|
73
|
+
If the optional `help_message` attribute is specified, `help greet` will also display it:
|
|
74
|
+
|
|
75
|
+
```txt
|
|
76
|
+
irb(main):001> help greet
|
|
77
|
+
Greets the user with the given name.
|
|
78
|
+
|
|
79
|
+
Usage: greet <name>
|
|
80
|
+
```
|
|
81
|
+
|
|
82
|
+
## Helper methods
|
|
83
|
+
|
|
84
|
+
Helper methods are designed to be used as Ruby methods, such as `my_helper(arg, kwarg: val).foo`.
|
|
85
|
+
|
|
86
|
+
The main use case of helper methods is to provide shortcuts for users, providing quick and easy access to
|
|
87
|
+
frequently used operations or components within the IRB session. For example, a helper method might simplify
|
|
88
|
+
the process of fetching and displaying specific configuration settings or data structures that would otherwise
|
|
89
|
+
require multiple steps to access.
|
|
90
|
+
|
|
91
|
+
### Example
|
|
92
|
+
|
|
93
|
+
```rb
|
|
94
|
+
# This only loads the minimum components required to define and register a helper method.
|
|
95
|
+
# It does not load the entire IRB, nor does it initialize it.
|
|
96
|
+
require "irb/helper_method"
|
|
97
|
+
|
|
98
|
+
class MyHelper < IRB::HelperMethod::Base
|
|
99
|
+
description "This is a test helper"
|
|
100
|
+
|
|
101
|
+
def execute(arg, kwarg:)
|
|
102
|
+
"arg: #{arg}, kwarg: #{kwarg}"
|
|
103
|
+
end
|
|
104
|
+
end
|
|
105
|
+
|
|
106
|
+
IRB::HelperMethod.register(:my_helper, MyHelper)
|
|
107
|
+
```
|
|
108
|
+
|
|
109
|
+
As long as the above code is loaded before the IRB session is started, such as in a loaded library or a user's `.irbrc` file, `my_helper` will be accessible to the user.
|
|
110
|
+
|
|
111
|
+
```txt
|
|
112
|
+
irb(main):001> my_helper("foo", kwarg: "bar").upcase
|
|
113
|
+
=> "ARG: FOO, KWARG: BAR"
|
|
114
|
+
```
|
|
115
|
+
|
|
116
|
+
The registered helper methods will also be listed in the help message's `Helper methods` section:
|
|
117
|
+
|
|
118
|
+
```txt
|
|
119
|
+
Helper methods
|
|
120
|
+
conf Returns the current context.
|
|
121
|
+
my_helper This is a test helper
|
|
122
|
+
```
|