Server.accept will block (wait) until a new connection happens. Once the callback (in the form of a Ruby block) completes the thread will end. So it starts a potentially infinite number of threads but only one per connection and each one is terminated pretty quickly. This is a pretty common way to write a server that can handle multiple simultaneous connections.
Ruby (and most languages) evaluates the arguments before passing them through to the function. So it first evaluates server.accept, which blocks until a new connection, then passes the return value through to Thread.new which spawns the new thread.
The parameters to Thread.new are just passed straight through to the block.
Comments
Wait, did she just start an infinite number of threads in a loop, or is ruby awesome in ways I didn't know?
Server.accept will block (wait) until a new connection happens. Once the callback (in the form of a Ruby block) completes the thread will end. So it starts a potentially infinite number of threads but only one per connection and each one is terminated pretty quickly. This is a pretty common way to write a server that can handle multiple simultaneous connections.
Soo the parameter to Thread.new is a function that blocks on the parent thread, before a new thread is even created?
Ruby (and most languages) evaluates the arguments before passing them through to the function. So it first evaluates server.accept, which blocks until a new connection, then passes the return value through to Thread.new which spawns the new thread.
The parameters to Thread.new are just passed straight through to the block.
Ahhh! I thought it was passing the accept function and not its result. Been too long, ruby! Thanks guys :)
Yep, that's exactly what's happening. A little confusing because ruby doesn't require brackets for zero argument function calls.