2013-01-11 62 views
1

我感到困惑的$變量的EventMachine的這部分代碼是如何工作的(strip_opString#sub方法):

def receive_data(data) 
     @buf = @buf ? @buf << data : data 

     while (@buf && [email protected]) 
     case @parse_state 
     when AWAITING_CONTROL_LINE 
      case @buf 
      when PUB_OP 
      ctrace('PUB OP', strip_op($&)) if NATSD::Server.trace_flag? 
      return connect_auth_timeout if @auth_pending 
      @buf = $' 
      @parse_state = AWAITING_MSG_PAYLOAD 
      @msg_sub, @msg_reply, @msg_size = $1, $3, $4.to_i 

有哪些含義$&$'$1等?

+0

看到這個:http://jimneath.org/2010/01/04/cryptic-ruby-global-variables-and-their-meanings.html –

+0

這些都記錄在這裏和那裏的互聯網周圍,但他們的形式使很難搜索。檢查「[魔術$前綴在Ruby中的變量;是否有一個完整的參考?](http://stackoverflow.com/questions/3746615/magic-prefixed-variables-in-ruby-is-there-a-complete-參考 - 某處)「和http://www.cs.auckland.ac.nz/references/ruby/stdlib/libdoc/English/rdoc/index.html –

回答

4

那些持有最後一個正則表達式匹配的部分。 $&:匹配的子串,$':匹配後的子串,$1:匹配的第一個捕獲的子串。

+0

謝謝你的回答! – harryz