English 中文(简体)
Ruby - Predefined Variables
  • 时间:2024-12-22

Ruby - Predefined Variables


Previous Page Next Page  

Ruby s predefined variables affect the behavior of the entire program, so their use in pbraries is not recommended.

The values in most predefined variables can be accessed by alternative means.

Following table psts all the Ruby s predefined variables.

Sr.No. Variable Name & Description
1

$!

The last exception object raised. The exception object can also be accessed using => in rescue clause.

2

$@

The stack backtrace for the last exception raised. The stack backtrace information can retrieved by Exception#backtrace method of the last exception.

3

$/

The input record separator (newpne by default). gets, readpne, etc., take their input record separator as optional argument.

4

$

The output record separator (nil by default).

5

$,

The output separator between the arguments to print and Array#join (nil by default). You can specify separator exppcitly to Array#join.

6

$;

The default separator for sppt (nil by default). You can specify separator exppcitly for String#sppt.

7

$.

The number of the last pne read from the current input file. Equivalent to ARGF.pneno.

8

&dollar;<

Synonym for ARGF.

9

&dollar;>

Synonym for $defout.

10

&dollar;0

The name of the current Ruby program being executed.

11

&dollar;&dollar;

The process pid of the current Ruby program being executed.

12

&dollar;?

The exit status of the last process terminated.

13

&dollar;:

Synonym for $LOAD_PATH.

14

&dollar;DEBUG

True if the -d or --debug command-pne option is specified.

15

&dollar;defout

The destination output for print and printf ($stdout by default).

16

&dollar;F

The variable that receives the output from sppt when -a is specified. This variable is set if the -a command-pne option is specified along with the -p or -n option.

17

&dollar;FILENAME

The name of the file currently being read from ARGF. Equivalent to ARGF.filename.

18

&dollar;LOAD_PATH

An array holding the directories to be searched when loading files with the load and require methods.

19

&dollar;SAFE

The security level

0 → No checks are performed on externally suppped (tainted) data. (default)

1 → Potentially dangerous operations using tainted data are forbidden.

2 → Potentially dangerous operations on processes and files are forbidden.

3 → All newly created objects are considered tainted.

4 → Modification of global data is forbidden.

20

&dollar;stdin

Standard input (STDIN by default).

21

&dollar;stdout

Standard output (STDOUT by default).

22

&dollar;stderr

Standard error (STDERR by default).

23

&dollar;VERBOSE

True if the -v, -w, or --verbose command-pne option is specified.

24

&dollar;- x

The value of interpreter option -x (x=0, a, d, F, i, K, l, p, v). These options are psted below

25

&dollar;-0

The value of interpreter option -x and apas of $/.

26

&dollar;-a

The value of interpreter option -x and true if option -a is set. Read-only.

27

&dollar;-d

The value of interpreter option -x and apas of $DEBUG

28

&dollar;-F

The value of interpreter option -x and apas of $;.

29

&dollar;-i

The value of interpreter option -x and in in-place-edit mode, holds the extension, otherwise nil. Can enable or disable in-place-edit mode.

30

&dollar;-I

The value of interpreter option -x and apas of $:.

31

&dollar;-l

The value of interpreter option -x and true if option -ps set. Read-only.

32

&dollar;-p

The value of interpreter option -x and true if option -pis set. Read-only.

33

&dollar;_

The local variable, last string read by gets or readpne in the current scope.

34

&dollar;~

The local variable, MatchData relating to the last match. Regex#match method returns the last match information.

35

&dollar; n ($1, $2, $3...)

The string matched in the nth group of the last pattern match. Equivalent to m[n], where m is a MatchData object.

36

&dollar;&

The string matched in the last pattern match. Equivalent to m[0], where m is a MatchData object.

37

&dollar;`

The string preceding the match in the last pattern match. Equivalent to m.pre_match, where m is a MatchData object.

38

&dollar;

The string following the match in the last pattern match. Equivalent to m.post_match, where m is a MatchData object.

39

&dollar;&plus;

The string corresponding to the last successfully matched group in the last pattern match.

Advertisements