Ruby: What does $1 mean?

According to Avdi Grimm from RubyTapas

$1 is a global variable which can be used in later code:

 if "foobar" =~ /foo(.*)/ then 
    puts "The matching word was #{$1}"
 end

Output:

“The matching word was bar”

In short, $1, $2, $… are the global-variables used by some of the ruby library functions specially concerning REGEX to let programmers use the findings in later codes.

See this for such more variables available in Ruby.

Leave a Comment

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)