diff --git a/doc/globals.rdoc b/doc/globals.rdoc index 1d7cda69f9..556e433dcb 100644 --- a/doc/globals.rdoc +++ b/doc/globals.rdoc @@ -1,69 +1,419 @@ -# -*- mode: rdoc; coding: utf-8; fill-column: 74; -*- +== Pre-Defined Global Variables -== Pre-defined global variables +Some of the pre-defined global variables have synonyms +that are available via module Engish. +For each of those, the \English synonym is given. -$!:: The Exception object set by Kernel#raise. -$@:: The same as $!.backtrace. -$~:: The information about the last match in the current scope (thread-local and frame-local). -$&:: The string matched by the last successful match. -$`:: The string to the left of the last successful match. -$':: The string to the right of the last successful match. -$+:: The highest group matched by the last successful match. -$1:: The Nth group of the last successful match. May be > 1. -$=:: This variable is no longer effective. Deprecated. -$/:: The input record separator, newline by default. Aliased to $-0. -$\:: The output record separator for Kernel#print and IO#write. Default is +nil+. -$,:: The output field separator for Kernel#print and Array#join. Non-nil $, will be deprecated. -$;:: The default separator for String#split. Non-nil $; will be deprecated. Aliased to $-F. -$.:: The current input line number of the last file that was read. -$<:: The same as ARGF. -$>:: The default output stream for Kernel#print and Kernel#printf. $stdout by default. -$_:: The last input line of string by gets or readline. -$0:: Contains the name of the script being executed. May be assignable. -$*:: The same as ARGV. -$$:: The process number of the Ruby running this script. Same as Process.pid. -$?:: The status of the last executed child process (thread-local). -$LOAD_PATH:: Load path for searching Ruby scripts and extension libraries used - by Kernel#load and Kernel#require. Aliased to $: and $-I. - Has a singleton method $LOAD_PATH.resolve_feature_path(feature) - that returns [+:rb+ or +:so+, path], which resolves the feature to - the path the original Kernel#require method would load. -$LOADED_FEATURES:: The array contains the module names loaded by require. - Aliased to $". -$DEBUG:: The debug flag, which is set by the -d switch. Enabling debug - output prints each exception raised to $stderr (but not its - backtrace). Setting this to a true value enables debug output as - if -d were given on the command line. Setting this to a false - value disables debug output. Aliased to $-d. -$FILENAME:: Current input filename from ARGF. Same as ARGF.filename. -$stderr:: The current standard error output. -$stdin:: The current standard input. -$stdout:: The current standard output. -$VERBOSE:: The verbose flag, which is set by the -w or -v switch. - Setting this to a true value enables warnings as if -w or -v were given - on the command line. Setting this to +nil+ disables warnings, - including from Kernel#warn. Aliased to $-v and $-w. -$-a:: True if option -a is set. Read-only variable. -$-i:: In in-place-edit mode, this variable holds the extension, otherwise +nil+. -$-l:: True if option -l is set. Read-only variable. -$-p:: True if option -p is set. Read-only variable. +To use the module: -== Pre-defined global constants + require 'English' -STDIN:: The standard input. The default value for $stdin. -STDOUT:: The standard output. The default value for $stdout. -STDERR:: The standard error output. The default value for $stderr. -ENV:: The hash contains current environment variables. -ARGF:: The virtual concatenation of the files given on command line (or from $stdin if no files were given). -ARGV:: An Array of command line arguments given for the script. -DATA:: The file object of the script, pointing just after __END__. -TOPLEVEL_BINDING:: The Binding of the top level scope. -RUBY_VERSION:: The Ruby language version. -RUBY_RELEASE_DATE:: The release date string. -RUBY_PLATFORM:: The platform identifier. -RUBY_PATCHLEVEL:: The patchlevel for this Ruby. If this is a development build of Ruby the patchlevel will be -1. -RUBY_REVISION:: The GIT commit hash for this Ruby. -RUBY_COPYRIGHT:: The copyright string for Ruby. -RUBY_ENGINE:: The name of the Ruby implementation. -RUBY_ENGINE_VERSION:: The version of the Ruby implementation. -RUBY_DESCRIPTION:: The same as ruby --version, a String describing various aspects of the Ruby implementation. +=== Exceptions + +==== $! (\Exception) + +Contains the Exception object set by Kernel#raise: + + begin + raise RuntimeError.new('Boo!') + rescue RuntimeError + p $! + end + +Output: + + # + +English - $ERROR_INFO + +==== $@ (Backtrace) + +Same as $!.backtrace; +returns an array of backtrace positions: + + begin + raise RuntimeError.new('Boo!') + rescue RuntimeError + pp $@.take(4) + end + +Output: + + ["(irb):338:in `'", + "/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 - $ERROR_POSITION. + +=== 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]. + +==== $~ (\MatchData) + +MatchData object created from the match; +thread-local and frame-local. + +English - $LAST_MATCH_INFO. + +==== $& (Matched Substring) + +The matched string. + +English - $MATCH. + +==== $` (Pre-Match Substring) + +The string to the left of the match. + +English - $PREMATCH. + +==== $' (Post-Match Substring) + +The string to the right of the match. + +English - $POSTMATCH. + +==== $+ (Last Matched Group) + +The last group matched. + +English - $LAST_PAREN_MATCH. + +==== $1, $2, \Etc. (Matched Group) + +For $_n_ the _nth_ group of the match. + +No \English. + +=== Separators + +==== $/ (Input Record Separator) + +An input record separator, initially newline. + +English - $INPUT_RECORD_SEPARATOR, $RS. + +Aliased as $-0. + +==== $; (Input Field Separator) + +An input field separator, initially +nil+. + +English - $FIELD_SEPARATOR, $FS. + +Aliased as $-F. + +==== $\\ (Output Record Separator) + +An output record separator, initially +nil+. + +English - $OUTPUT_RECORD_SEPARATOR, $ORS. + +=== Streams + +==== $stdin (Standard Input) + +The current standard input stream; initially: + + $stdin # => #> + +==== $stdout (Standard Output) + +The current standard output stream; initially: + + $stdout # => #> + +==== $stderr (Standard Error) + +The current standard error stream; initially: + + $stderr # => #> + +==== $< (\ARGF or $stdin) + +Points to stream ARGF if not empty, else to stream $stdin; read-only. + +English - $DEFAULT_INPUT. + +==== $> (Default Standard Output) + +An output stream, initially $stdout. + +English - $DEFAULT_OUTPUT + +==== $. (Input Position) + +The input position (line number) in the most recently read stream. + +English - $INPUT_LINE_NUMBER, $NR + +==== $_ (Last Read Line) + +The line (string) from the most recently read stream. + +English - $LAST_READ_LINE. + +=== Processes + +==== $0 + +Initially, contains the name of the script being executed; +may be reassigned. + +==== $* (\ARGV) + +Points to ARGV. + +English - $ARGV. + +==== $$ (Process ID) + +The process ID of the current process. Same as Process.pid. + +English - $PROCESS_ID, $PID. + +==== $? (Child Status) + +Initially +nil+, otherwise the Process::Status object +created for the most-recently exited child process; +thread-local. + +English - $CHILD_STATUS. + +==== $LOAD_PATH (Load Path) + +Contains the array of paths to be searched +by Kernel#load and Kernel#require. + +Singleton method $LOAD_PATH.resolve_feature_path(feature) +returns: + +- [:rb, _path_], where +path+ is the path to the Ruby file + to be loaded for the given +feature+. +- [:so+ _path_], 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 $: and $-I. + +==== $LOADED_FEATURES + +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 $". + +=== Debugging + +==== $FILENAME + +The value returned by method ARGF.filename. + +==== $DEBUG + +Initially +true+ if command-line option -d or --debug is given, +otherwise initially +false+; +may be set to either value in the running program. + +When +true+, prints each raised exception to $stderr. + +Aliased as $-d. + +==== $VERBOSE + +Initially +true+ if command-line option -v or -w 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 $-v and $-w. + +=== Other Variables + +==== $-a + +Whether command-line option -a was given; read-only. + +==== $-i + +Contains the extension given with command-line option -i, +or +nil+ if none. + +==== $-l + +Whether command-line option -l was set; read-only. + +==== $-p + +Whether command-line option -p was given; read-only. + +=== Deprecated + +==== $= + +==== $, + +== Pre-Defined Global Constants + +== Streams + +==== STDIN + +The standard input stream (the default value for $stdin): + + STDIN # => #> + +==== STDOUT + +The standard output stream (the default value for $stdout): + + STDOUT # => #> + +==== STDERR + +The standard error stream (the default value for $stderr): + + STDERR # => #> + +=== Enviroment + +==== 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 $stdin if no files were given. + +==== ARGV + +An array of the given command-line arguments. + +==== TOPLEVEL_BINDING + +The Binding of the top level scope: + + TOPLEVEL_BINDING # => # + +==== RUBY_VERSION + +The Ruby version: + + RUBY_VERSION # => "3.2.2" + +==== RUBY_RELEASE_DATE + +The release date string: + + RUBY_RELEASE_DATE # => "2023-03-30" + +==== RUBY_PLATFORM + +The platform identifier: + + RUBY_PLATFORM # => "x86_64-linux" + +==== RUBY_PATCHLEVEL + +The integer patch level for this Ruby: + + RUBY_PATCHLEVEL # => 53 + +For a development build the patch level will be -1. + +==== RUBY_REVISION + +The git commit hash for this Ruby: + + RUBY_REVISION # => "e51014f9c05aa65cbf203442d37fef7c12390015" + +==== RUBY_COPYRIGHT + +The copyright string: + + RUBY_COPYRIGHT + # => "ruby - Copyright (C) 1993-2023 Yukihiro Matsumoto" + +==== RUBY_ENGINE + +The name of the Ruby implementation: + + RUBY_ENGINE # => "ruby" + +==== RUBY_ENGINE_VERSION + +The version of the Ruby implementation: + + RUBY_ENGINE_VERSION # => "3.2.2" + +==== RUBY_DESCRIPTION + +The description of the Ruby implementation: + + RUBY_DESCRIPTION + # => "ruby 3.2.2 (2023-03-30 revision e51014f9c0) [x86_64-linux]" + +=== Embedded \Data + +==== DATA + +Defined if and only if the program has this line: + + __END__ + +When defined, DATA is a File object +containing the lines following the __END__, +positioned at the first of those lines: + + p DATA + DATA.each_line { |line| p line } + __END__ + Foo + Bar + Baz + +Output: + + # + "Foo\n" + "Bar\n" + "Baz\n"