what is “$:” in ruby?

2019-03-26 17:54发布

Usually be seen in .gemspec file. eg. i18n.gemspec.

$: << File.expand_path('../lib', __FILE__)

3条回答
我只想做你的唯一
2楼-- · 2019-03-26 18:34
Pre-defined variables
$!         The exception information message set by 'raise'.
$@         Array of backtrace of the last exception thrown.
$&         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.
$~         The information about the last match in the current scope.
$=         The flag for case insensitive, nil by default.
$/         The input record separator, newline by default.
$\         The output record separator for the print and IO#write. Default is nil.
$,         The output field separator for the print and Array#join.
$;         The default separator for String#split.
$.         The current input line number of the last file that was read.
$<         The virtual concatenation file of the files given on command line (or from $stdin if no files were given).
$>         The default output for print, 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.
$*         Command line arguments given for the script sans args.
$$         The process number of the Ruby running this script.
$?         The status of the last executed child process.
$:         Load path for scripts and binary modules by load or require.
$"         The array contains the module names loaded by require.
$DEBUG     The status of the -d switch.
$FILENAME  Current input file from $<. Same as $<.filename.
$LOAD_PATH The alias to the $:.
$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 -v switch.
$-0        The alias to $/.
$-a        True if option -a is set. Read-only variable.
$-d        The alias to $DEBUG.
$-F        The alias to $;.
$-i        In in-place-edit mode, this variable holds the extension, otherwise nil.
$-I        The alias to $:.
$-l        True if option -l is set. Read-only variable.
$-p        True if option -p is set. Read-only variable.
$-v        The alias to $VERBOSE.
$    -w        True if option -w is set.

the above shortcuts are an unfortunate Perl anachronism! There are "English Names" available for some of them, which are self-explanatory, and which should be used if available.

see also: http://ruby.runpaint.org/globals

see also: http://www.zenspider.com/Languages/Ruby/QuickRef.html

查看更多
趁早两清
3楼-- · 2019-03-26 18:39

$: is equivalent to $LOAD_PATH, i.e. the list of directories that you can require files from without giving a more specific path. You'll find the Ruby QuickRef useful for other predefined variables you may run into.

查看更多
混吃等死
4楼-- · 2019-03-26 18:41

$: is the load path and $LOAD_PATH is its alias. When you load/require libraries/files within your ruby application, ruby searches in the directories listed in $: for them. With $: << File.expand_path('../lib', __FILE__) you add a specific directory to the load path.

查看更多
登录 后发表回答