2012-05-02 42 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