2012-05-02 46 views
0

我試圖更好地理解涉及EventMachine的以下幾行代碼。努力學習Ruby是一種艱辛的方式。EventMachine和一些Ruby語法

EventMachine.run在這段代碼中做了什麼?

什麼| chunk |在這種情況下呢?

另外,'while line'會做什麼?是行Ruby語法?我似乎無法找到與此相關的任何內容。

#create an HTTP request ob to URL above with user authorization 
EventMachine.run do 
    http = EventMachine::HttpRequest.new(url).get :head => { 'Authorization' => [ user, password ] } 

# Initiate an empty string for the buffer 
    buffer = "" 

# Read the stream by line 
    http.stream do |chunk| 
    buffer += chunk 
    while line = buffer.slice!(/.+\r?\n/) #cut each line at newline 
     handle_tweet JSON.parse(line) #send each tweet object to handle_tweet method 
    end 

    end 
end 
+0

你應該學習一些ruby語言的基礎。 WikiBooks中的[Ruby Tutorial](http://en.wikibooks.org/wiki/Ruby_Programming)可能會有所幫助。 – Flexoid

+0

這就是我想要做的。 thx爲鏈接。 – fokusfocus

回答

0

您可以使用調試器。諸如ruby-debug之類的調試器的目的是爲了讓你在Ruby程序執行時看到「內部」發生了什麼。 或者您可以使用記錄器。 Logger提供了一個簡單但複雜的日誌實用程序,您可以使用它來顯示輸出消息。

+0

如何使用記錄器?謝謝。 – fokusfocus

+0

所有您需要了解的記錄器:http://www.ruby-doc.org/stdlib-1.9.3/libdoc/logger/rdoc/Logger.html – suvankar