Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Ruby global variable double dollar sign

Tags:

ruby

I discovered a special global variable in Ruby. It's $$ and it's always a number. But what does it mean? Even in the documentation I can't find anything.

like image 524
Sessho Avatar asked Jun 15 '26 08:06

Sessho


2 Answers

It's a preset variable that represents the current process' ID. It's documented in http://ruby-doc.org/stdlib-2.3.1/libdoc/English/rdoc/English.html.

Process.pid is an alternate, or use:

require 'English'
my_pid = $PROCESS_ID

In general, the short-cut globals are discouraged from use because they're like magical incantations or visual noise unless you know what they are.

like image 66
the Tin Man Avatar answered Jun 17 '26 23:06

the Tin Man


That's one of the pre-defined Ruby global variables.

All globals are prefixed with $, and in this case $$ represents the current process ID. This was inherited from Perl.

Some things aren't very easy to search for, and $$ is one of them, so it's understandable why you couldn't find it easily.

like image 22
tadman Avatar answered Jun 17 '26 23:06

tadman