What are Ruby's numbered global variables

后端 未结 2 1924
深忆病人
深忆病人 2021-02-14 11:03

What do the values $1, $2, $\', $` mean in Ruby?

2条回答
  •  深忆病人
    2021-02-14 11:32

    The non-numbered ones are listed here:

    https://www.zenspider.com/ruby/quickref.html#pre-defined-variables

    $1, $2 ... $N refer to matches in a regex capturing group.

    So:

    "ab:cd" =~ /([a-z]+):([a-z]+)/
    

    Would yield

    $1 = "ab"
    $2 = "cd"
    

提交回复
热议问题