From: "Iñaki Baz Castillo" Date: 2011-08-23T04:55:26+09:00 Subject: Re: Possble bug in Ruby parser (Fixnum#times within "case" statement) 2011/8/22 Ryan Davis : > Needless complexity is needless and will _always_ bite you sooner or later. > > Compare: > > def receive_data(data) >  @buffer << data > >  @parser.reset >  @parser_nbytes = 0 >  parse_headers >  get_body >  process_request > end > > catch :done do >  loop do >    receive_data get_data >  end > end This is a very simplistic and urrealistic approach for a parsing on top of TCP in which you can receive incomplete data in each TCP package so must wait until the event reactor (EventMachine in this case) calls receive_data(data) again. For example, the method parse_headers() reads data from the buffer and determines if it's a complete protocol message (just headers, as in a HTTP request) or not. If so, it returns and sets @state=:body, true so the case statement gets true and the loop continues, but now it goes to get_body() method. This method, similary, checks whether there are Content-Length bytes in the buffer. If so, it has the complete body, sets @state=:finished and returns true. If not, it returns false so the while ends. Next time the TCP server receives pending data, the reactor calls receive_data(data) again so the case enters again in get_body() method and reads, probably, the entire buffer to complete the body. It's much more complex than your code which cannot work in a real TCP server in which nobody can expect to receive the entire protocol message in a single TCP package. -- Iñaki Baz Castillo