irb 1.15.1 → 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 +2 -0
- 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/completion.rb +31 -36
- data/lib/irb/debug.rb +2 -2
- data/lib/irb/easter-egg.rb +3 -1
- data/lib/irb/input-method.rb +17 -11
- data/lib/irb/inspector.rb +1 -1
- data/lib/irb/pager.rb +1 -1
- data/lib/irb/ruby-lex.rb +25 -0
- data/lib/irb/source_finder.rb +1 -1
- data/lib/irb/version.rb +2 -2
- data/lib/irb.rb +36 -16
- metadata +11 -7
- data/Rakefile +0 -52
- data/bin/console +0 -6
- data/bin/setup +0 -6
- data/irb.gemspec +0 -46
data/doc/Index.md
ADDED
|
@@ -0,0 +1,705 @@
|
|
|
1
|
+
# IRB
|
|
2
|
+
|
|
3
|
+
[](https://badge.fury.io/rb/irb)
|
|
4
|
+
[](https://github.com/ruby/irb/actions/workflows/test.yml)
|
|
5
|
+
|
|
6
|
+
## Overview
|
|
7
|
+
|
|
8
|
+
IRB stands for "Interactive Ruby" and is a tool to interactively execute Ruby expressions read from the standard input. The `irb` command from your shell will start the interpreter.
|
|
9
|
+
|
|
10
|
+
IRB provides a shell-like interface that supports user interaction with the Ruby interpreter. It operates as a *read-eval-print loop* ([REPL](https://en.wikipedia.org/wiki/Read%E2%80%93eval%E2%80%93print_loop)) that:
|
|
11
|
+
|
|
12
|
+
- **Reads** each character as you type. You can modify the IRB context to change the way input works. See [Input](#label-Input).
|
|
13
|
+
- **Evaluates** the code each time it has read a syntactically complete passage.
|
|
14
|
+
- **Prints** after evaluating. You can modify the IRB context to change the way output works. See [Output](#label-Output).
|
|
15
|
+
|
|
16
|
+
## Installation
|
|
17
|
+
|
|
18
|
+
> **Note**
|
|
19
|
+
>
|
|
20
|
+
> IRB is a default gem of Ruby, so you shouldn't need to install it separately. However, if you're using Ruby 2.6 or later and want to upgrade/install a specific version of IRB, follow these steps.
|
|
21
|
+
|
|
22
|
+
To install it with `bundler`, add this line to your application's Gemfile:
|
|
23
|
+
|
|
24
|
+
```ruby
|
|
25
|
+
gem 'irb'
|
|
26
|
+
```
|
|
27
|
+
|
|
28
|
+
Then execute:
|
|
29
|
+
|
|
30
|
+
```console
|
|
31
|
+
$ bundle
|
|
32
|
+
```
|
|
33
|
+
|
|
34
|
+
Or install it directly with:
|
|
35
|
+
|
|
36
|
+
```console
|
|
37
|
+
$ gem install irb
|
|
38
|
+
```
|
|
39
|
+
|
|
40
|
+
## Usage
|
|
41
|
+
|
|
42
|
+
> **Note**
|
|
43
|
+
>
|
|
44
|
+
> We're working hard to match Pry's variety of powerful features in IRB. Track our progress or find contribution ideas in [COMPARED_WITH_PRY.md](./COMPARED_WITH_PRY.md).
|
|
45
|
+
|
|
46
|
+
### Starting IRB
|
|
47
|
+
|
|
48
|
+
You can start a fresh IRB session by typing `irb` in your terminal. In the session, you can evaluate Ruby expressions or prototype small Ruby scripts. Input is executed when it is syntactically complete.
|
|
49
|
+
|
|
50
|
+
```console
|
|
51
|
+
$ irb
|
|
52
|
+
irb(main):001> 1 + 2
|
|
53
|
+
=> 3
|
|
54
|
+
irb(main):002* class Foo
|
|
55
|
+
irb(main):003* def foo
|
|
56
|
+
irb(main):004* puts 1
|
|
57
|
+
irb(main):005* end
|
|
58
|
+
irb(main):006> end
|
|
59
|
+
=> :foo
|
|
60
|
+
irb(main):007> Foo.new.foo
|
|
61
|
+
1
|
|
62
|
+
=> nil
|
|
63
|
+
```
|
|
64
|
+
|
|
65
|
+
### The `binding.irb` Breakpoint
|
|
66
|
+
|
|
67
|
+
If you use Ruby 2.5 or later versions, you can use `binding.irb` in your program as breakpoints. Once `binding.irb` is evaluated, a new IRB session starts with the surrounding context:
|
|
68
|
+
|
|
69
|
+
```console
|
|
70
|
+
$ ruby test.rb
|
|
71
|
+
|
|
72
|
+
From: test.rb @ line 2 :
|
|
73
|
+
|
|
74
|
+
1: def greet(word)
|
|
75
|
+
=> 2: binding.irb
|
|
76
|
+
3: puts "Hello #{word}"
|
|
77
|
+
4: end
|
|
78
|
+
5:
|
|
79
|
+
6: greet("World")
|
|
80
|
+
|
|
81
|
+
irb(main):001> word
|
|
82
|
+
=> "World"
|
|
83
|
+
irb(main):002> exit
|
|
84
|
+
Hello World
|
|
85
|
+
```
|
|
86
|
+
|
|
87
|
+
### Debugging
|
|
88
|
+
|
|
89
|
+
You can use IRB as a debugging console with `debug.gem` with these options:
|
|
90
|
+
|
|
91
|
+
- In `binding.irb`, use the `debug` command to start an `irb:rdbg` session with access to all `debug.gem` commands.
|
|
92
|
+
- Use the `RUBY_DEBUG_IRB_CONSOLE=1` environment variable to make `debug.gem` use IRB as the debugging console.
|
|
93
|
+
|
|
94
|
+
To learn more about debugging with IRB, see [Debugging with IRB](#label-Debugging+with+IRB).
|
|
95
|
+
|
|
96
|
+
## Startup
|
|
97
|
+
|
|
98
|
+
At startup, IRB:
|
|
99
|
+
|
|
100
|
+
1. Interprets (as Ruby code) the content of the [configuration file](rdoc-ref:Configurations.md) (if given).
|
|
101
|
+
2. Constructs the initial session context from [hash IRB.conf](#label-Hash+IRB.conf) and from default values; the hash content may have been affected by [command-line options](#command-line-options), and by direct assignments in the configuration file.
|
|
102
|
+
3. Assigns the context to variable `conf`.
|
|
103
|
+
4. Assigns command-line arguments to variable `ARGV`.
|
|
104
|
+
5. Prints the prompt.
|
|
105
|
+
6. Puts the content of the [initialization script](#label-Initialization+script) onto the IRB shell, just as if it were user-typed commands.
|
|
106
|
+
|
|
107
|
+
## Command Line
|
|
108
|
+
|
|
109
|
+
On the command line, all options precede all arguments; the first item that is not recognized as an option is treated as an argument, as are all items that follow.
|
|
110
|
+
|
|
111
|
+
### Command-Line Options
|
|
112
|
+
|
|
113
|
+
Many command-line options affect entries in hash `IRB.conf`, which in turn affect the initial configuration of the IRB session.
|
|
114
|
+
|
|
115
|
+
Details of the options are described in relevant subsections below. A cursory list of IRB command-line options may be seen in the [help message](https://raw.githubusercontent.com/ruby/irb/master/lib/irb/lc/help-message), which is also displayed if you use command-line option `--help`.
|
|
116
|
+
|
|
117
|
+
If you are interested in a specific option, consult the [index](rdoc-ref:COMMAND_LINE_OPTIONS.md).
|
|
118
|
+
|
|
119
|
+
### Command-Line Arguments
|
|
120
|
+
|
|
121
|
+
Command-line arguments are passed to IRB in array `ARGV`:
|
|
122
|
+
|
|
123
|
+
```console
|
|
124
|
+
$ irb --noscript Foo Bar Baz
|
|
125
|
+
irb(main):001> ARGV
|
|
126
|
+
=> ["Foo", "Bar", "Baz"]
|
|
127
|
+
irb(main):002> exit
|
|
128
|
+
$
|
|
129
|
+
```
|
|
130
|
+
|
|
131
|
+
Command-line option `--` causes everything that follows to be treated as arguments, even those that look like options:
|
|
132
|
+
|
|
133
|
+
```console
|
|
134
|
+
$ irb --noscript -- --noscript -- Foo Bar Baz
|
|
135
|
+
irb(main):001> ARGV
|
|
136
|
+
=> ["--noscript", "--", "Foo", "Bar", "Baz"]
|
|
137
|
+
irb(main):002> exit
|
|
138
|
+
$
|
|
139
|
+
```
|
|
140
|
+
|
|
141
|
+
## Commands
|
|
142
|
+
|
|
143
|
+
The following commands are available in IRB. Use the `help` command to see the list of available commands.
|
|
144
|
+
|
|
145
|
+
```txt
|
|
146
|
+
Help
|
|
147
|
+
help List all available commands. Use `help <command>` to get information about a specific command.
|
|
148
|
+
|
|
149
|
+
IRB
|
|
150
|
+
context Displays current configuration.
|
|
151
|
+
exit Exit the current irb session.
|
|
152
|
+
exit! Exit the current process.
|
|
153
|
+
irb_load Load a Ruby file.
|
|
154
|
+
irb_require Require a Ruby file.
|
|
155
|
+
source Loads a given file in the current session.
|
|
156
|
+
irb_info Show information about IRB.
|
|
157
|
+
history Shows the input history. `-g [query]` or `-G [query]` allows you to filter the output.
|
|
158
|
+
disable_irb Disable binding.irb.
|
|
159
|
+
|
|
160
|
+
Workspace
|
|
161
|
+
cwws Show the current workspace.
|
|
162
|
+
chws Change the current workspace to an object.
|
|
163
|
+
workspaces Show workspaces.
|
|
164
|
+
pushws Push an object to the workspace stack.
|
|
165
|
+
popws Pop a workspace from the workspace stack.
|
|
166
|
+
cd Move into the given object or leave the current context.
|
|
167
|
+
|
|
168
|
+
Multi-irb (DEPRECATED)
|
|
169
|
+
irb Start a child IRB.
|
|
170
|
+
jobs List of current sessions.
|
|
171
|
+
fg Switches to the session of the given number.
|
|
172
|
+
kill Kills the session with the given number.
|
|
173
|
+
|
|
174
|
+
Debugging
|
|
175
|
+
debug Start the debugger of debug.gem.
|
|
176
|
+
break Start the debugger of debug.gem and run its `break` command.
|
|
177
|
+
catch Start the debugger of debug.gem and run its `catch` command.
|
|
178
|
+
next Start the debugger of debug.gem and run its `next` command.
|
|
179
|
+
delete Start the debugger of debug.gem and run its `delete` command.
|
|
180
|
+
step Start the debugger of debug.gem and run its `step` command.
|
|
181
|
+
continue Start the debugger of debug.gem and run its `continue` command.
|
|
182
|
+
finish Start the debugger of debug.gem and run its `finish` command.
|
|
183
|
+
backtrace Start the debugger of debug.gem and run its `backtrace` command.
|
|
184
|
+
info Start the debugger of debug.gem and run its `info` command.
|
|
185
|
+
|
|
186
|
+
Misc
|
|
187
|
+
edit Open a file or source location.
|
|
188
|
+
measure `measure` enables the mode to measure processing time. `measure :off` disables it.
|
|
189
|
+
copy Copy expression output to clipboard
|
|
190
|
+
|
|
191
|
+
Context
|
|
192
|
+
show_doc Look up documentation with RI.
|
|
193
|
+
ls Show methods, constants, and variables.
|
|
194
|
+
show_source Show the source code of a given method, class/module, or constant.
|
|
195
|
+
whereami Show the source code around binding.irb again.
|
|
196
|
+
|
|
197
|
+
Helper methods
|
|
198
|
+
conf Returns the current IRB context.
|
|
199
|
+
|
|
200
|
+
Aliases
|
|
201
|
+
$ Alias for `show_source`
|
|
202
|
+
@ Alias for `whereami`
|
|
203
|
+
```
|
|
204
|
+
|
|
205
|
+
## Configure IRB
|
|
206
|
+
|
|
207
|
+
See [Configurations](rdoc-ref:Configurations.md) for more details.
|
|
208
|
+
|
|
209
|
+
## Input
|
|
210
|
+
|
|
211
|
+
This section describes the features that allow you to change the way IRB input works; see also [Output](#output).
|
|
212
|
+
|
|
213
|
+
### Input Command History
|
|
214
|
+
|
|
215
|
+
By default, IRB stores a history of up to 1000 input commands in a file named `.irb_history`. The history file will be in the same directory as the [configuration file](#label-Configuration+File) if one is found, or in `~/` otherwise.
|
|
216
|
+
|
|
217
|
+
A new IRB session creates the history file if it does not exist and appends to the file if it does exist.
|
|
218
|
+
|
|
219
|
+
You can change the filepath by adding to your configuration file:
|
|
220
|
+
`IRB.conf[:HISTORY_FILE] = *filepath*`, where *filepath* is a string filepath.
|
|
221
|
+
|
|
222
|
+
During the session, method `conf.history_file` returns the filepath, and method `conf.history_file = *new_filepath*` copies the history to the file at *new_filepath*, which becomes the history file for the session.
|
|
223
|
+
|
|
224
|
+
You can change the number of commands saved by adding to your configuration file: `IRB.conf[:SAVE_HISTORY] = *n*`, where *n* is one of:
|
|
225
|
+
|
|
226
|
+
- Positive integer: the number of commands to be saved.
|
|
227
|
+
- Negative integer: all commands are to be saved.
|
|
228
|
+
- Zero or `nil`: no commands are to be saved.
|
|
229
|
+
|
|
230
|
+
During the session, you can use methods `conf.save_history` or `conf.save_history=` to retrieve or change the count.
|
|
231
|
+
|
|
232
|
+
### Command Aliases
|
|
233
|
+
|
|
234
|
+
By default, IRB defines several command aliases:
|
|
235
|
+
|
|
236
|
+
```console
|
|
237
|
+
irb(main):001> conf.command_aliases
|
|
238
|
+
=> {:"$"=>:show_source, :"@"=>:whereami}
|
|
239
|
+
```
|
|
240
|
+
|
|
241
|
+
You can change the initial aliases in the configuration file with:
|
|
242
|
+
|
|
243
|
+
```ruby
|
|
244
|
+
IRB.conf[:COMMAND_ALIASES] = {foo: :show_source, bar: :whereami}
|
|
245
|
+
```
|
|
246
|
+
|
|
247
|
+
You can replace the current aliases at any time with configuration method `conf.command_aliases=`; because `conf.command_aliases` is a hash, you can modify it.
|
|
248
|
+
|
|
249
|
+
### End-of-File
|
|
250
|
+
|
|
251
|
+
By default, `IRB.conf[:IGNORE_EOF]` is `false`, which means that typing the end-of-file character `Ctrl-D` causes the session to exit.
|
|
252
|
+
|
|
253
|
+
You can reverse that behavior by adding `IRB.conf[:IGNORE_EOF] = true` to the configuration file.
|
|
254
|
+
|
|
255
|
+
During the session, method `conf.ignore_eof?` returns the setting, and method `conf.ignore_eof = *boolean*` sets it.
|
|
256
|
+
|
|
257
|
+
### SIGINT
|
|
258
|
+
|
|
259
|
+
By default, `IRB.conf[:IGNORE_SIGINT]` is `true`, which means that typing the interrupt character `Ctrl-C` does not cause the session to exit.
|
|
260
|
+
|
|
261
|
+
You can reverse that behavior by adding `IRB.conf[:IGNORE_SIGINT] = false` to the configuration file.
|
|
262
|
+
|
|
263
|
+
During the session, method `conf.ignore_sigint?` returns the setting, and method `conf.ignore_sigint = *boolean*` sets it.
|
|
264
|
+
|
|
265
|
+
### Automatic Completion
|
|
266
|
+
|
|
267
|
+
By default, IRB enables [automatic completion](https://en.wikipedia.org/wiki/Autocomplete#In_command-line_interpreter):
|
|
268
|
+
|
|
269
|
+
To cycle through the completion suggestions, use the tab key (and shift-tab to reverse).
|
|
270
|
+
|
|
271
|
+
You can disable it by either of these:
|
|
272
|
+
|
|
273
|
+
- Adding `IRB.conf[:USE_AUTOCOMPLETE] = false` to the configuration file.
|
|
274
|
+
- Giving command-line option `--noautocomplete` (`--autocomplete` is the default).
|
|
275
|
+
|
|
276
|
+
Method `conf.use_autocomplete?` returns `true` if automatic completion is enabled, `false` otherwise.
|
|
277
|
+
|
|
278
|
+
The setting may not be changed during the session.
|
|
279
|
+
|
|
280
|
+
### Type Based Completion
|
|
281
|
+
|
|
282
|
+
IRB's default completion `IRB::RegexpCompletor` uses Regexp. IRB offers an experimental completion `IRB::TypeCompletor` that uses type analysis.
|
|
283
|
+
|
|
284
|
+
#### How to Enable IRB::TypeCompletor
|
|
285
|
+
|
|
286
|
+
Install [ruby/repl_type_completor](https://github.com/ruby/repl_type_completor/) with:
|
|
287
|
+
|
|
288
|
+
```console
|
|
289
|
+
$ gem install repl_type_completor
|
|
290
|
+
```
|
|
291
|
+
|
|
292
|
+
Or add these lines to your project's Gemfile.
|
|
293
|
+
|
|
294
|
+
```ruby
|
|
295
|
+
gem 'irb'
|
|
296
|
+
gem 'repl_type_completor', group: [:development, :test]
|
|
297
|
+
```
|
|
298
|
+
|
|
299
|
+
Now you can use type-based completion by:
|
|
300
|
+
|
|
301
|
+
- Running IRB with the `--type-completor` option
|
|
302
|
+
|
|
303
|
+
```console
|
|
304
|
+
$ irb --type-completor
|
|
305
|
+
```
|
|
306
|
+
|
|
307
|
+
- Or writing this line to IRB's rc-file (e.g., `~/.irbrc`)
|
|
308
|
+
|
|
309
|
+
```ruby
|
|
310
|
+
IRB.conf[:COMPLETOR] = :type # default is :regexp
|
|
311
|
+
```
|
|
312
|
+
|
|
313
|
+
- Or setting the environment variable `IRB_COMPLETOR`
|
|
314
|
+
|
|
315
|
+
```ruby
|
|
316
|
+
ENV['IRB_COMPLETOR'] = 'type'
|
|
317
|
+
IRB.start
|
|
318
|
+
```
|
|
319
|
+
|
|
320
|
+
To check if it's enabled, type `irb_info` into IRB and see the `Completion` section.
|
|
321
|
+
|
|
322
|
+
```console
|
|
323
|
+
irb(main):001> irb_info
|
|
324
|
+
...
|
|
325
|
+
# Enabled
|
|
326
|
+
Completion: Autocomplete, ReplTypeCompletor: 0.1.0, Prism: 0.18.0, RBS: 3.3.0
|
|
327
|
+
# Not enabled
|
|
328
|
+
Completion: Autocomplete, RegexpCompletor
|
|
329
|
+
...
|
|
330
|
+
```
|
|
331
|
+
|
|
332
|
+
If you have a `sig/` directory or `rbs_collection.lock.yaml` in the current directory, IRB will load it.
|
|
333
|
+
|
|
334
|
+
#### Advantage over Default IRB::RegexpCompletor
|
|
335
|
+
|
|
336
|
+
`IRB::TypeCompletor` can autocomplete chained methods, block parameters, and more if type information is available. These are some examples `IRB::RegexpCompletor` cannot complete.
|
|
337
|
+
|
|
338
|
+
```console
|
|
339
|
+
irb(main):001> 'Ruby'.upcase.chars.s # Array methods (sample, select, shift, size)
|
|
340
|
+
```
|
|
341
|
+
|
|
342
|
+
```console
|
|
343
|
+
irb(main):001> 10.times.map(&:to_s).each do |s|
|
|
344
|
+
irb(main):002> s.up # String methods (upcase, upcase!, upto)
|
|
345
|
+
```
|
|
346
|
+
|
|
347
|
+
```console
|
|
348
|
+
irb(main):001> class User < ApplicationRecord
|
|
349
|
+
irb(main):002> def foo
|
|
350
|
+
irb(main):003> sa # save, save!
|
|
351
|
+
```
|
|
352
|
+
|
|
353
|
+
As a trade-off, completion calculation takes more time than `IRB::RegexpCompletor`.
|
|
354
|
+
|
|
355
|
+
#### Difference between Steep's Completion
|
|
356
|
+
|
|
357
|
+
Compared with Steep, `IRB::TypeCompletor` has some differences and limitations.
|
|
358
|
+
```ruby
|
|
359
|
+
[0, 'a'].sample.
|
|
360
|
+
# Steep completes the intersection of Integer methods and String methods
|
|
361
|
+
# IRB::TypeCompletor completes both Integer and String methods
|
|
362
|
+
```
|
|
363
|
+
|
|
364
|
+
Some features like type narrowing are not implemented.
|
|
365
|
+
```ruby
|
|
366
|
+
def f(arg = [0, 'a'].sample)
|
|
367
|
+
if arg.is_a?(String)
|
|
368
|
+
arg. # Completes both Integer and String methods
|
|
369
|
+
```
|
|
370
|
+
|
|
371
|
+
Unlike other static type checkers, `IRB::TypeCompletor` uses runtime information to provide better completion.
|
|
372
|
+
|
|
373
|
+
```console
|
|
374
|
+
irb(main):001> a = [1]
|
|
375
|
+
=> [1]
|
|
376
|
+
irb(main):002> a.first. # Completes Integer methods
|
|
377
|
+
```
|
|
378
|
+
|
|
379
|
+
### Automatic Indentation
|
|
380
|
+
|
|
381
|
+
By default, IRB automatically indents lines of code to show structure (e.g., it indents the contents of a block).
|
|
382
|
+
|
|
383
|
+
The current setting is returned by the configuration method `conf.auto_indent_mode`.
|
|
384
|
+
|
|
385
|
+
The default initial setting is `true`:
|
|
386
|
+
|
|
387
|
+
```console
|
|
388
|
+
irb(main):001> conf.auto_indent_mode
|
|
389
|
+
=> true
|
|
390
|
+
irb(main):002* Dir.entries('.').select do |entry|
|
|
391
|
+
irb(main):003* entry.start_with?('R')
|
|
392
|
+
irb(main):004> end
|
|
393
|
+
=> ["README.md", "Rakefile"]
|
|
394
|
+
```
|
|
395
|
+
|
|
396
|
+
You can change the initial setting in the configuration file with:
|
|
397
|
+
|
|
398
|
+
```ruby
|
|
399
|
+
IRB.conf[:AUTO_INDENT] = false
|
|
400
|
+
```
|
|
401
|
+
|
|
402
|
+
Note that the *current* setting *may not* be changed in the IRB session.
|
|
403
|
+
|
|
404
|
+
### Input Method
|
|
405
|
+
|
|
406
|
+
The IRB input method determines how command input is read; by default, the input method for a session is IRB::RelineInputMethod unless the TERM environment variable is 'dumb', in which case the most simplistic input method is used.
|
|
407
|
+
|
|
408
|
+
You can set the input method by:
|
|
409
|
+
|
|
410
|
+
- Adding to the configuration file:
|
|
411
|
+
|
|
412
|
+
- `IRB.conf[:USE_SINGLELINE] = true` or `IRB.conf[:USE_MULTILINE] = false` sets the input method to IRB::ReadlineInputMethod.
|
|
413
|
+
- `IRB.conf[:USE_SINGLELINE] = false` or `IRB.conf[:USE_MULTILINE] = true` sets the input method to IRB::RelineInputMethod.
|
|
414
|
+
|
|
415
|
+
- Giving command-line options:
|
|
416
|
+
|
|
417
|
+
- `--singleline` or `--nomultiline` sets the input method to IRB::ReadlineInputMethod.
|
|
418
|
+
- `--nosingleline` or `--multiline` sets the input method to IRB::RelineInputMethod.
|
|
419
|
+
- `--nosingleline` together with `--nomultiline` sets the input to IRB::StdioInputMethod.
|
|
420
|
+
|
|
421
|
+
Method `conf.use_multiline?` and its synonym `conf.use_reline` return:
|
|
422
|
+
|
|
423
|
+
- `true` if option `--multiline` was given.
|
|
424
|
+
- `false` if option `--nomultiline` was given.
|
|
425
|
+
- `nil` if neither was given.
|
|
426
|
+
|
|
427
|
+
Method `conf.use_singleline?` and its synonym `conf.use_readline` return:
|
|
428
|
+
|
|
429
|
+
- `true` if option `--singleline` was given.
|
|
430
|
+
- `false` if option `--nosingleline` was given.
|
|
431
|
+
- `nil` if neither was given.
|
|
432
|
+
|
|
433
|
+
## Output
|
|
434
|
+
|
|
435
|
+
This section describes the features that allow you to change the way IRB output works; see also [Input](#label-Input).
|
|
436
|
+
|
|
437
|
+
### Return-Value Printing (Echoing)
|
|
438
|
+
|
|
439
|
+
By default, IRB prints (echoes) the values returned by all input commands.
|
|
440
|
+
|
|
441
|
+
You can change the initial behavior and suppress all echoing by:
|
|
442
|
+
|
|
443
|
+
- Adding to the configuration file: `IRB.conf[:ECHO] = false`. (The default value for this entry is `nil`, which means the same as `true`.)
|
|
444
|
+
- Giving command-line option `--noecho`. (The default is `--echo`.)
|
|
445
|
+
|
|
446
|
+
During the session, you can change the current setting with configuration method `conf.echo=` (set to `true` or `false`).
|
|
447
|
+
|
|
448
|
+
As stated above, by default IRB prints the values returned by all input commands; but IRB offers special treatment for values returned by assignment statements, which may be:
|
|
449
|
+
|
|
450
|
+
- Printed with truncation (to fit on a single line of output), which is the default; an ellipsis (`...` is suffixed, to indicate the truncation):
|
|
451
|
+
|
|
452
|
+
```console
|
|
453
|
+
irb(main):001> x = 'abc' * 100
|
|
454
|
+
> "abcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabc..."
|
|
455
|
+
```
|
|
456
|
+
|
|
457
|
+
- Printed in full (regardless of the length).
|
|
458
|
+
- Suppressed (not printed at all).
|
|
459
|
+
|
|
460
|
+
You can change the initial behavior by:
|
|
461
|
+
|
|
462
|
+
- Adding to the configuration file: `IRB.conf[:ECHO_ON_ASSIGNMENT] = false`. (The default value for this entry is `nil`, which means the same as `:truncate`.)
|
|
463
|
+
- Giving command-line option `--noecho-on-assignment` or `--echo-on-assignment`. (The default is `--truncate-echo-on-assignment`.)
|
|
464
|
+
|
|
465
|
+
During the session, you can change the current setting with configuration method `conf.echo_on_assignment=` (set to `true`, `false`, or `:truncate`).
|
|
466
|
+
|
|
467
|
+
By default, IRB formats returned values by calling method `inspect`.
|
|
468
|
+
|
|
469
|
+
You can change the initial behavior by:
|
|
470
|
+
|
|
471
|
+
- Adding to the configuration file: `IRB.conf[:INSPECT_MODE] = false`. (The default value for this entry is `true`.)
|
|
472
|
+
- Giving command-line option `--noinspect`. (The default is `--inspect`.)
|
|
473
|
+
|
|
474
|
+
During the session, you can change the setting using method `conf.inspect_mode=`.
|
|
475
|
+
|
|
476
|
+
### Multiline Output
|
|
477
|
+
|
|
478
|
+
By default, IRB prefixes a newline to a multiline response.
|
|
479
|
+
|
|
480
|
+
You can change the initial default value by adding to the configuration file:
|
|
481
|
+
|
|
482
|
+
```ruby
|
|
483
|
+
IRB.conf[:NEWLINE_BEFORE_MULTILINE_OUTPUT] = false
|
|
484
|
+
```
|
|
485
|
+
|
|
486
|
+
During a session, you can retrieve or set the value using methods `conf.newline_before_multiline_output?` and `conf.newline_before_multiline_output=`.
|
|
487
|
+
|
|
488
|
+
Examples:
|
|
489
|
+
|
|
490
|
+
```console
|
|
491
|
+
irb(main):001> conf.inspect_mode = false
|
|
492
|
+
=> false
|
|
493
|
+
irb(main):002> "foo\nbar"
|
|
494
|
+
=>
|
|
495
|
+
foo
|
|
496
|
+
bar
|
|
497
|
+
irb(main):003> conf.newline_before_multiline_output = false
|
|
498
|
+
=> false
|
|
499
|
+
irb(main):004> "foo\nbar"
|
|
500
|
+
=> foo
|
|
501
|
+
bar
|
|
502
|
+
```
|
|
503
|
+
|
|
504
|
+
### Evaluation History
|
|
505
|
+
|
|
506
|
+
By default, IRB saves no history of evaluations (returned values), and the related methods `conf.eval_history`, `_`, and `__` are undefined.
|
|
507
|
+
|
|
508
|
+
You can turn on that history and set the maximum number of evaluations to be stored:
|
|
509
|
+
|
|
510
|
+
- In the configuration file: add `IRB.conf[:EVAL_HISTORY] = *n*`. (Examples below assume that we've added `IRB.conf[:EVAL_HISTORY] = 5`.)
|
|
511
|
+
- In the session (at any time): `conf.eval_history = *n*`.
|
|
512
|
+
|
|
513
|
+
If `n` is zero, all evaluation history is stored.
|
|
514
|
+
|
|
515
|
+
Doing either of the above:
|
|
516
|
+
|
|
517
|
+
- Sets the maximum size of the evaluation history; defines method `conf.eval_history`, which returns the maximum size `n` of the evaluation history:
|
|
518
|
+
|
|
519
|
+
```console
|
|
520
|
+
irb(main):001> conf.eval_history = 5
|
|
521
|
+
=> 5
|
|
522
|
+
irb(main):002> conf.eval_history
|
|
523
|
+
=> 5
|
|
524
|
+
```
|
|
525
|
+
|
|
526
|
+
- Defines variable `_`, which contains the most recent evaluation, or `nil` if none; same as method `conf.last_value`:
|
|
527
|
+
|
|
528
|
+
```console
|
|
529
|
+
irb(main):003> _
|
|
530
|
+
=> 5
|
|
531
|
+
irb(main):004> :foo
|
|
532
|
+
=> :foo
|
|
533
|
+
irb(main):005> :bar
|
|
534
|
+
=> :bar
|
|
535
|
+
irb(main):006> _
|
|
536
|
+
=> :bar
|
|
537
|
+
irb(main):007> _
|
|
538
|
+
=> :bar
|
|
539
|
+
```
|
|
540
|
+
|
|
541
|
+
- Defines variable `__`:
|
|
542
|
+
|
|
543
|
+
- `__` unadorned: contains all evaluation history:
|
|
544
|
+
|
|
545
|
+
```console
|
|
546
|
+
irb(main):008> :foo
|
|
547
|
+
=> :foo
|
|
548
|
+
irb(main):009> :bar
|
|
549
|
+
=> :bar
|
|
550
|
+
irb(main):010> :baz
|
|
551
|
+
=> :baz
|
|
552
|
+
irb(main):011> :bat
|
|
553
|
+
=> :bat
|
|
554
|
+
irb(main):012> :bam
|
|
555
|
+
=> :bam
|
|
556
|
+
irb(main):013> __
|
|
557
|
+
=>
|
|
558
|
+
9 :bar
|
|
559
|
+
10 :baz
|
|
560
|
+
11 :bat
|
|
561
|
+
12 :bam
|
|
562
|
+
13 ...self-history...
|
|
563
|
+
```
|
|
564
|
+
|
|
565
|
+
Note that when the evaluation is multiline, it is displayed differently.
|
|
566
|
+
|
|
567
|
+
- `__[m]`:
|
|
568
|
+
|
|
569
|
+
- Positive `m`: contains the evaluation for the given line number, or `nil` if that line number is not in the evaluation history:
|
|
570
|
+
|
|
571
|
+
```console
|
|
572
|
+
irb(main):015> __[12]
|
|
573
|
+
=> :bam
|
|
574
|
+
irb(main):016> __[1]
|
|
575
|
+
=> nil
|
|
576
|
+
```
|
|
577
|
+
|
|
578
|
+
- Negative `m`: contains the `mth`-from-end evaluation, or `nil` if that evaluation is not in the evaluation history:
|
|
579
|
+
|
|
580
|
+
```console
|
|
581
|
+
irb(main):017> __[-3]
|
|
582
|
+
=> :bam
|
|
583
|
+
irb(main):018> __[-13]
|
|
584
|
+
=> nil
|
|
585
|
+
```
|
|
586
|
+
|
|
587
|
+
- Zero `m`: contains `nil`:
|
|
588
|
+
|
|
589
|
+
```console
|
|
590
|
+
irb(main):019> __[0]
|
|
591
|
+
=> nil
|
|
592
|
+
```
|
|
593
|
+
|
|
594
|
+
### Initialization Script
|
|
595
|
+
|
|
596
|
+
By default, the first command-line argument (after any options) is the path to a Ruby initialization script.
|
|
597
|
+
|
|
598
|
+
IRB reads the initialization script and puts its content onto the IRB shell, just as if it were user-typed commands.
|
|
599
|
+
|
|
600
|
+
Command-line option `--noscript` causes the first command-line argument to be treated as an ordinary argument (instead of an initialization script); `--script` is the default.
|
|
601
|
+
|
|
602
|
+
## Debugging with IRB
|
|
603
|
+
|
|
604
|
+
Starting from version 1.8.0, IRB offers a powerful integration with `debug.gem`, providing a debugging experience similar to `pry-byebug`.
|
|
605
|
+
|
|
606
|
+
After hitting a `binding.irb` breakpoint, you can activate the debugger with the `debug` command. Alternatively, if the `debug` method is already defined in the current scope, you can call `irb_debug`.
|
|
607
|
+
|
|
608
|
+
```console
|
|
609
|
+
From: test.rb @ line 3 :
|
|
610
|
+
|
|
611
|
+
1:
|
|
612
|
+
2: def greet(word)
|
|
613
|
+
=> 3: binding.irb
|
|
614
|
+
4: puts "Hello #{word}"
|
|
615
|
+
5: end
|
|
616
|
+
6:
|
|
617
|
+
7: greet("World")
|
|
618
|
+
|
|
619
|
+
irb(main):001> debug
|
|
620
|
+
irb:rdbg(main):002>
|
|
621
|
+
```
|
|
622
|
+
|
|
623
|
+
Once activated, the prompt's header changes from `irb` to `irb:rdbg`, enabling you to use any of `debug.gem`'s [commands](https://github.com/ruby/debug#debug-command-on-the-debug-console):
|
|
624
|
+
|
|
625
|
+
```console
|
|
626
|
+
irb:rdbg(main):002> info # use info command to see available variables
|
|
627
|
+
%self = main
|
|
628
|
+
_ = nil
|
|
629
|
+
word = "World"
|
|
630
|
+
irb:rdbg(main):003> next # use next command to move to the next line
|
|
631
|
+
[1, 7] in test.rb
|
|
632
|
+
1|
|
|
633
|
+
2| def greet(word)
|
|
634
|
+
3| binding.irb
|
|
635
|
+
=> 4| puts "Hello #{word}"
|
|
636
|
+
5| end
|
|
637
|
+
6|
|
|
638
|
+
7| greet("World")
|
|
639
|
+
=>#0 Object#greet(word="World") at test.rb:4
|
|
640
|
+
#1 <main> at test.rb:7
|
|
641
|
+
irb:rdbg(main):004>
|
|
642
|
+
```
|
|
643
|
+
|
|
644
|
+
Simultaneously, you maintain access to IRB's commands, such as `show_source`:
|
|
645
|
+
|
|
646
|
+
```console
|
|
647
|
+
irb:rdbg(main):004> show_source greet
|
|
648
|
+
|
|
649
|
+
From: test.rb:2
|
|
650
|
+
|
|
651
|
+
def greet(word)
|
|
652
|
+
binding.irb
|
|
653
|
+
puts "Hello #{word}"
|
|
654
|
+
end
|
|
655
|
+
```
|
|
656
|
+
|
|
657
|
+
### More about `debug.gem`
|
|
658
|
+
|
|
659
|
+
`debug.gem` offers many advanced debugging features that simple REPLs can't provide, including:
|
|
660
|
+
|
|
661
|
+
- Step-debugging
|
|
662
|
+
- Frame navigation
|
|
663
|
+
- Setting breakpoints with commands
|
|
664
|
+
- Thread control
|
|
665
|
+
- ...and many more
|
|
666
|
+
|
|
667
|
+
To learn about these features, refer to `debug.gem`'s [commands list](https://github.com/ruby/debug#debug-command-on-the-debug-console).
|
|
668
|
+
|
|
669
|
+
In the `irb:rdbg` session, the `help` command also displays all commands from `debug.gem`.
|
|
670
|
+
|
|
671
|
+
### Advantages Over `debug.gem`'s Console
|
|
672
|
+
|
|
673
|
+
This integration offers several benefits over `debug.gem`'s native console:
|
|
674
|
+
|
|
675
|
+
1. Access to handy IRB commands like `show_source` or `show_doc`.
|
|
676
|
+
2. Support for multi-line input.
|
|
677
|
+
3. Symbol shortcuts such as `@` (`whereami`) and `$` (`show_source`).
|
|
678
|
+
4. Autocompletion.
|
|
679
|
+
5. Customizable prompt.
|
|
680
|
+
|
|
681
|
+
However, there are some limitations to be aware of:
|
|
682
|
+
|
|
683
|
+
1. `binding.irb` doesn't support `pre` and `do` arguments like [binding.break](https://github.com/ruby/debug#bindingbreak-method).
|
|
684
|
+
2. As IRB [doesn't currently support remote-connection](https://github.com/ruby/irb/issues/672), it can't be used with `debug.gem`'s remote debugging feature.
|
|
685
|
+
3. Access to the previous return value via the underscore `_` is not supported.
|
|
686
|
+
|
|
687
|
+
## Encodings
|
|
688
|
+
|
|
689
|
+
Command-line option `-E *ex*[:*in*]` sets initial external (ex) and internal (in) encodings.
|
|
690
|
+
|
|
691
|
+
Command-line option `-U` sets both to UTF-8.
|
|
692
|
+
|
|
693
|
+
## Contributing
|
|
694
|
+
|
|
695
|
+
See [CONTRIBUTING.md](https://github.com/ruby/irb/blob/main/CONTRIBUTING.md) for more information.
|
|
696
|
+
|
|
697
|
+
## Extending IRB
|
|
698
|
+
|
|
699
|
+
IRB `v1.13.0` and later versions allow users/libraries to extend its functionality through official APIs.
|
|
700
|
+
|
|
701
|
+
For more information, visit [EXTEND_IRB.md](rdoc-ref:EXTEND_IRB.md).
|
|
702
|
+
|
|
703
|
+
## License
|
|
704
|
+
|
|
705
|
+
The gem is available as open source under the terms of the [2-Clause BSD License](https://opensource.org/licenses/BSD-2-Clause).
|