Skip to content

Instantly share code, notes, and snippets.

@darthrighteous
Forked from jooeycheng/ruby_dollar_signs.txt
Last active October 14, 2022 13:52
Show Gist options
  • Save darthrighteous/40eaf78b3b0ade93e397373c09a21fe3 to your computer and use it in GitHub Desktop.
Save darthrighteous/40eaf78b3b0ade93e397373c09a21fe3 to your computer and use it in GitHub Desktop.
ruby dollar sign $ global variable
$: (Dollar Colon) is basically a shorthand version of $LOAD_PATH.
contains an array of paths that your script will search through when using require.
$0 (Dollar Zero) contains the name of the ruby program being run. This is typically the script name.
$* (Dollar Splat) is basically shorthand for ARGV. $* contains the command line arguments that were passed to the script.
$? (Dollar Question Mark) returns the exit status of the last child process to finish.
$$ (Dollar Dollar) returns the process number of the program currently being ran.
$~ (Dollar Tilde) contains the MatchData from the previous successful pattern match.
$1, $2, $3, $4 etc represent the content of the previous successful pattern match.
$& (Dollar Ampersand) contains the matched string from the previous successful pattern match.
$+ (Dollar Plus) contains the last match from the previous successful pattern match.
$` (Dollar Backtick) contains the string before the actual matched string of the previous successful pattern match.
$’ (Dollar Apostrophe) contains the string after the actual matched string of the previous successful pattern match.
$! (Dollar Bang) contains the Exception that was passed to raise.
$@ (Dollar At Symbol) contains the backtrace for the last Exception raised.
Alternate Explaination (from diff site)
$! latest error message
$@ location of error
$_ string last read by gets
$. line number last read by interpreter
$& string last matched by regexp
$~ the last regexp match, as an array of subexpressions
$n the nth subexpression in the last match (same as $~[n])
$= case-insensitivity flag
$/ input record separator
$\ output record separator
$0 the name of the ruby script file
$* the command line arguments
$$ interpreter's process ID
$? exit status of last executed child process
Alternatives using ruby/English https://github.com/ruby/English
$: -> $LOAD_PATH
$" -> $LOADED_FEATURES
$0 -> $PROGRAM_NAME
$! -> $ERROR_INFO
$@ -> $ERROR_POSITION
$; -> $FIELD_SEPARATOR / $FS
$, -> $OUTPUT_FIELD_SEPARATOR / $OFS
$/ -> $INPUT_RECORD_SEPARATOR / $RS
$\ -> $OUTPUT_RECORD_SEPARATOR / $ORS
$. -> $INPUT_LINE_NUMBER / $NR
$_ -> $LAST_READ_LINE
$> -> $DEFAULT_OUTPUT
$< -> $DEFAULT_INPUT
$$ -> $PROCESS_ID / $PID
$? -> $CHILD_STATUS
$~ -> $LAST_MATCH_INFO
$= -> $IGNORECASE
$* -> $ARGV / ARG
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment