0

I'm trying to get a better understanding of the following lines of code involving EventMachine. Trying to learn Ruby the hard way.

What does EventMachine.run does in this code?

What does |chunk| means in this case?

Also, what does 'while line' does? Is line a Ruby syntax? I couldn't seem to find anything related to that.

#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
fokusfocus
  • 195
  • 1
  • 3
  • 15

1 Answers1

0

You may use debugger. The purpose of a debugger such as ruby-debug is to allow you to see what is going on “inside” a Ruby program while it executes. OR you may use logger. Logger provides a simple but sophisticated logging utility that you can use to display output messages.

suvankar
  • 1,548
  • 1
  • 20
  • 28
  • all you need to know about logger: http://www.ruby-doc.org/stdlib-1.9.3/libdoc/logger/rdoc/Logger.html – suvankar May 03 '12 at 07:15