Что означает токен "$$" в Ruby? - PullRequest
4 голосов
/ 09 июля 2011

Я вижу эту переменную в image_temp_file.rb в makeTempname () в библиотеке mini_magick.

Ответы [ 3 ]

5 голосов
/ 09 июля 2011

$ начинает ссылку на глобальную переменную. Программы, как правило, определяют что-то вроде $ name , а система заранее устанавливает ряд информационных и управляющих ссылок.

В частности,

$$ - это идентификатор процесса.

 
    $name program-defined global variable
    $!  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
4 голосов
/ 09 июля 2011

Это идентификатор процесса интерпретатора Ruby, выполняющего скрипт, в котором вы находитесь. Например:

[/tmp] Ψ irb
ruby> $$
 => 16045                          # We're in process id 16045.
ruby> ^Z
[1]+  Stopped irb                  # Let's stop irb so we can
                                   # verify that it's the right pid.

[/tmp] Ψ ps aux | grep -inr 16045  # grep across all processes.
80:johnf    16045  ... irb         # There it is!
0 голосов
/ 09 июля 2011

$$ соответствует идентификатору процесса работающей программы.

...