Cover by Michael Fitzgerald

Safari, the world’s most comprehensive technology and business learning platform.

Find the exact information you need to solve a problem on the fly, or go deeper to master the technologies and skills you need to succeed

Start Free Trial

No credit card required

O'Reilly logo

Predefined Variables

Table A-4 lists all of Ruby's predefined variables.

Table A-4. Predefined variables

Predefined variable

Description

$!

Exception-information message containing the last exception raised. raise sets this variable. Access with => in a rescue clause.

$@

Stack backtrace of the last exception, retrievable via Exception#backtrace.

$&

String matched by the last successful pattern match in this scope, or nil if the last pattern match failed. Same as m[0] where m is a MatchData object. Read only. Local.

$`

String preceding whatever was matched by the last successful pattern match in the current scope, or nil if the last pattern match failed. Same as m.pre_match where m is a MatchData object. Read only. Local.

$'

String following whatever was matched by the last successful pattern match in the current scope, or nil if the last pattern match failed. Same as m.post_match where m is a MatchData object. Read only. Local.

$+

Last bracket matched by the last successful search pattern, or nil if the last pattern match failed. Useful if you don't know which of a set of alternative patterns matched. Read only. Local.

$1, $2...

Subpattern from the corresponding set of parentheses in the last successful pattern matched, not counting patterns matched in nested blocks that have been exited already, or nil if the last pattern match failed. Same as m[n] where m is a MatchData object. Read only. Local.

$~

Information about the last match in the current scope. Regex#match ...

Find the exact information you need to solve a problem on the fly, or go deeper to master the technologies and skills you need to succeed

Start Free Trial

No credit card required