【问题标题】:EventMachine and some Ruby syntaxEventMachine 和一些 Ruby 语法
【发布时间】: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


【解决方案1】:

您可以使用调试器。诸如 ruby​​-debug 之类的调试器的目的是让您在 Ruby 程序执行时看到它“内部”发生了什么。 或者你可以使用记录器。 Logger 提供了一个简单但复杂的日志实用程序,您可以使用它来显示输出消息。

【讨论】:

猜你喜欢
  • 1970-01-01
  • 2011-04-29
  • 1970-01-01
  • 2011-04-15
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多