summaryrefslogtreecommitdiff
path: root/doc/globals.rdoc
blob: 1b51bb1b368411fe5333391c4b83dae34c514ab0 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
= Pre-Defined Global Variables

Some of the pre-defined global variables have synonyms
that are available via module English.
For each of those, the \English synonym is given.

To use the module:

  require 'English'

== Exceptions

=== <tt>$!</tt> (\Exception)

Contains the Exception object set by Kernel#raise:

  begin
    raise RuntimeError.new('Boo!')
  rescue RuntimeError
    p $!
  end

Output:

  #<RuntimeError: Boo!>

English - <tt>$ERROR_INFO</tt>

=== <tt>$@</tt> (Backtrace)

Same as <tt>$!.backtrace</tt>;
returns an array of backtrace positions:

  begin
    raise RuntimeError.new('Boo!')
  rescue RuntimeError
    pp $@.take(4)
  end

Output:

  ["(irb):338:in `<top (required)>'",
   "/snap/ruby/317/lib/ruby/3.2.0/irb/workspace.rb:119:in `eval'",
   "/snap/ruby/317/lib/ruby/3.2.0/irb/workspace.rb:119:in `evaluate'",
   "/snap/ruby/317/lib/ruby/3.2.0/irb/context.rb:502:in `evaluate'"]

English - <tt>$ERROR_POSITION</tt>.

== Pattern Matching

These global variables store information about the most recent
successful match in the current scope.

For details and examples,
see {Regexp Global Variables}[rdoc-ref:Regexp@Global+Variables].

=== <tt>$~</tt> (\MatchData)

MatchData object created from the match;
thread-local and frame-local.

English - <tt>$LAST_MATCH_INFO</tt>.

=== <tt>$&</tt> (Matched Substring)

The matched string.

English - <tt>$MATCH</tt>.

=== <tt>$`</tt> (Pre-Match Substring)

The string to the left of the match.

English - <tt>$PREMATCH</tt>.

=== <tt>$'</tt> (Post-Match Substring)

The string to the right of the match.

English - <tt>$POSTMATCH</tt>.

=== <tt>$+</tt> (Last Matched Group)

The last group matched.

English - <tt>$LAST_PAREN_MATCH</tt>.

=== <tt>$1</tt>, <tt>$2</tt>, \Etc. (Matched Group)

For <tt>$_n_</tt> the _nth_ group of the match.

No \English.

== Separators

=== <tt>$/</tt> (Input Record Separator)

An input record separator, initially newline.

English - <tt>$INPUT_RECORD_SEPARATOR</tt>, <tt>$RS</tt>.

Aliased as <tt>$-0</tt>.

=== <tt>$;</tt> (Input Field Separator)

An input field separator, initially +nil+.

English - <tt>$FIELD_SEPARATOR</tt>, <tt>$FS</tt>.

Aliased as <tt>$-F</tt>.

=== <tt>$\\</tt> (Output Record Separator)

An output record separator, initially +nil+.

English - <tt>$OUTPUT_RECORD_SEPARATOR</tt>, <tt>$ORS</tt>.

== Streams

=== <tt>$stdin</tt> (Standard Input)

The current standard input stream; initially:

  $stdin # => #<IO:<STDIN>>

=== <tt>$stdout</tt> (Standard Output)

The current standard output stream; initially:

  $stdout # => #<IO:<STDOUT>>

=== <tt>$stderr</tt> (Standard Error)

The current standard error stream; initially:

  $stderr # => #<IO:<STDERR>>

=== <tt>$<</tt> (\ARGF or $stdin)

Points to stream ARGF if not empty, else to stream $stdin; read-only.

English - <tt>$DEFAULT_INPUT</tt>.

=== <tt>$></tt> (Default Standard Output)

An output stream, initially <tt>$stdout</tt>.

English - <tt>$DEFAULT_OUTPUT

=== <tt>$.</tt> (Input Position)

The input position (line number) in the most recently read stream.

English - <tt>$INPUT_LINE_NUMBER</tt>, <tt>$NR</tt>

=== <tt>$_</tt> (Last Read Line)

The line (string) from the most recently read stream.

English - <tt>$LAST_READ_LINE</tt>.

== Processes

=== <tt>$0</tt>

Initially, contains the name of the script being executed;
may be reassigned.

=== <tt>$*</tt> (\ARGV)

Points to ARGV.

English - <tt>$ARGV</tt>.

=== <tt>$$</tt> (Process ID)

The process ID of the current process. Same as Process.pid.

English - <tt>$PROCESS_ID</tt>, <tt>$PID</tt>.

=== <tt>$?</tt> (Child Status)

Initially +nil+, otherwise the Process::Status object
created for the most-recently exited child process;
thread-local.

English - <tt>$CHILD_STATUS</tt>.

=== <tt>$LOAD_PATH</tt> (Load Path)

Contains the array of paths to be searched
by Kernel#load and Kernel#require.

Singleton method <tt>$LOAD_PATH.resolve_feature_path(feature)</tt>
returns:

- <tt>[:rb, _path_]</tt>, where +path+ is the path to the Ruby file
  to be loaded for the given +feature+.
- <tt>[:so+ _path_]</tt>, where +path+ is the path to the shared object file
  to be loaded for the given +feature+.
- +nil+ if there is no such +feature+ and +path+.

Examples:

  $LOAD_PATH.resolve_feature_path('timeout')
  # => [:rb, "/snap/ruby/317/lib/ruby/3.2.0/timeout.rb"]
  $LOAD_PATH.resolve_feature_path('date_core')
  # => [:so, "/snap/ruby/317/lib/ruby/3.2.0/x86_64-linux/date_core.so"]
  $LOAD_PATH.resolve_feature_path('foo')
  # => nil

Aliased as <tt>$:</tt> and <tt>$-I</tt>.

=== <tt>$LOADED_FEATURES</tt>

Contains an array of the paths to the loaded files:

  $LOADED_FEATURES.take(10)
  # =>
  ["enumerator.so",
   "thread.rb",
   "fiber.so",
   "rational.so",
   "complex.so",
   "ruby2_keywords.rb",
   "/snap/ruby/317/lib/ruby/3.2.0/x86_64-linux/enc/encdb.so",
   "/snap/ruby/317/lib/ruby/3.2.0/x86_64-linux/enc/trans/transdb.so",
   "/snap/ruby/317/lib/ruby/3.2.0/x86_64-linux/rbconfig.rb",
   "/snap/ruby/317/lib/ruby/3.2.0/rubygems/compatibility.rb"]

Aliased as <tt>$"</tt>.

== Debugging

=== <tt>$FILENAME</tt>

The value returned by method ARGF.filename.

=== <tt>$DEBUG</tt>

Initially +true+ if command-line option <tt>-d</tt> or <tt>--debug</tt> is given,
otherwise initially +false+;
may be set to either value in the running program.

When +true+, prints each raised exception to <tt>$stderr</tt>.

Aliased as <tt>$-d</tt>.

=== <tt>$VERBOSE</tt>

Initially +true+ if command-line option <tt>-v</tt> or <tt>-w</tt> is given,
otherwise initially +false+;
may be set to either value, or to +nil+, in the running program.

When +true+, enables Ruby warnings.

When +nil+, disables warnings, including those from Kernel#warn.

Aliased as <tt>$-v</tt> and <tt>$-w</tt>.

== Other Variables

=== <tt>$-a</tt>

Whether command-line option <tt>-a</tt> was given; read-only.

=== <tt>$-i</tt>

Contains the extension given with command-line option <tt>-i</tt>,
or +nil+ if none.

An alias of ARGF.inplace_mode.

=== <tt>$-l</tt>

Whether command-line option <tt>-l</tt> was set; read-only.

=== <tt>$-p</tt>

Whether command-line option <tt>-p</tt> was given; read-only.

== Deprecated

=== <tt>$=</tt>

=== <tt>$,</tt>

= Pre-Defined Global Constants

= Streams

=== <tt>STDIN</tt>

The standard input stream (the default value for <tt>$stdin</tt>):

  STDIN # => #<IO:<STDIN>>

=== <tt>STDOUT</tt>

The standard output stream (the default value for <tt>$stdout</tt>):

  STDOUT # => #<IO:<STDOUT>>

=== <tt>STDERR</tt>

The standard error stream (the default value for <tt>$stderr</tt>):

  STDERR # => #<IO:<STDERR>>

== Environment

=== ENV

A hash of the contains current environment variables names and values:

  ENV.take(5)
  # =>
  [["COLORTERM", "truecolor"],
   ["DBUS_SESSION_BUS_ADDRESS", "unix:path=/run/user/1000/bus"],
   ["DESKTOP_SESSION", "ubuntu"],
   ["DISPLAY", ":0"],
   ["GDMSESSION", "ubuntu"]]

=== ARGF

The virtual concatenation of the files given on the command line, or from
<tt>$stdin</tt> if no files were given, <tt>"-"</tt> is given, or after
all files have been read.

=== <tt>ARGV</tt>

An array of the given command-line arguments.

=== <tt>TOPLEVEL_BINDING</tt>

The Binding of the top level scope:

  TOPLEVEL_BINDING # => #<Binding:0x00007f58da0da7c0>

=== <tt>RUBY_VERSION</tt>

The Ruby version:

  RUBY_VERSION # => "3.2.2"

=== <tt>RUBY_RELEASE_DATE</tt>

The release date string:

  RUBY_RELEASE_DATE # => "2023-03-30"

=== <tt>RUBY_PLATFORM</tt>

The platform identifier:

  RUBY_PLATFORM # => "x86_64-linux"

=== <tt>RUBY_PATCHLEVEL</tt>

The integer patch level for this Ruby:

  RUBY_PATCHLEVEL # => 53

For a development build the patch level will be -1.

=== <tt>RUBY_REVISION</tt>

The git commit hash for this Ruby:

  RUBY_REVISION # => "e51014f9c05aa65cbf203442d37fef7c12390015"

=== <tt>RUBY_COPYRIGHT</tt>

The copyright string:

  RUBY_COPYRIGHT
  # => "ruby - Copyright (C) 1993-2023 Yukihiro Matsumoto"

=== <tt>RUBY_ENGINE</tt>

The name of the Ruby implementation:

  RUBY_ENGINE # => "ruby"

=== <tt>RUBY_ENGINE_VERSION</tt>

The version of the Ruby implementation:

  RUBY_ENGINE_VERSION # => "3.2.2"

=== <tt>RUBY_DESCRIPTION</tt>

The description of the Ruby implementation:

  RUBY_DESCRIPTION
  # => "ruby 3.2.2 (2023-03-30 revision e51014f9c0) [x86_64-linux]"

== Embedded \Data

=== <tt>DATA</tt>

Defined if and only if the program has this line:

  __END__

When defined, <tt>DATA</tt> is a File object
containing the lines following the <tt>__END__</tt>,
positioned at the first of those lines:

  p DATA
  DATA.each_line { |line| p line }
  __END__
  Foo
  Bar
  Baz

Output:

  #<File:t.rb>
  "Foo\n"
  "Bar\n"
  "Baz\n"