What do the values $1, $2, $', $` mean in Ruby?
Global Variables are variables that may be accessed from anywhere in the program regardless of scope. They're denoted by beginning with a $ (dollar sign) character. However, the use of global variables is often considered "un-Ruby," and you will rarely see them.
Ruby variables are locations which hold data to be used in the programs. Each variable has a different name. These variable names are based on some naming conventions. Unlike other programming languages, there is no need to declare a variable in Ruby. A prefix is needed to indicate it.
Ruby has four types of variable scope, local, global, instance and class. In addition, Ruby has one constant type.
They're captures from the most recent pattern match (just as in Perl; Ruby initially lifted a lot of syntax from Perl, although it's largely gotten over it by now :). $1
, $2
, etc. refer to parenthesized captures within a regex: given /a(.)b(.)c/
, $1
will be the character between a
and b
and $2
the character between b
and c
. $`
and $'
mean the strings before and after the string that matched the entire regex (which is itself in $&
), respectively.
There is actually some sense to these, if only historically; you can find it in perldoc perlvar
, which generally does a good job of documenting the intended mnemonics and history of Perl variables, and mostly still applies to the globals in Ruby. The numbered captures are replacements for the capture backreference regex syntax (\1
, \2
, etc.); Perl switched from the former to the latter somewhere in the 3.x versions, because using the backreference syntax outside of the regex complicated parsing too much. (By the time Perl 5 rolled around, the parser had been sufficiently rewritten that the syntax was again available, and promptly reused for references/"pointers". Ruby opted for using a name-quote :
instead, which is closer to the Lisp and Smalltalk style; since Ruby started out as a Perl-alike with Smalltalk-style OO, this made more sense linguistically.) The same applies to $&
, which in historical regex syntax is simply &
(but you can't use that outside the replacement part of a substitution, so it became a variable $&
instead). $`
and $'
are both "cutesy": "back-quote" and "forward-quote" from the matched string.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With