【发布时间】:2012-05-02 05:32:42
【问题描述】:
我试图更好地理解以下涉及 EventMachine 的代码行。努力学习 Ruby。
EventMachine.run 在这段代码中做了什么?
|chunk| 是什么意思?在这种情况下意味着什么?
另外,'while line' 有什么作用? 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
【问题讨论】:
-
你应该学习一些Ruby语言的基础。来自 WikiBooks 的 Ruby Tutorial 可能会有所帮助。
-
这就是我想要做的。谢谢链接。
标签: ruby syntax line eventmachine