Rack is a bridge between web servers and web applications. Most of the web frameworks in Ruby are based on Rack. In this article, I'd like to explore and demystify how its rackup tool works.

Diving deep into its source code revealed that some eval wizardry is being used. Here's a sneak-peak of its beauty.

# We cannot use instance_eval(String) as that would resolve constants differently.

# We want to build a variant of TOPLEVEL_BINDING with self as a Rack::Builder instance.

# Evaluate the given +builder_script+ string in the context of

I'll get to it later and explain how it works and why it's needed.

For now, let's get started with a "Hello World" example:

Running this with rackup will start Webrick and serve our little "Hello World" app on port 9292.

It looks like running the rackup command somehow started the server and provided the run method to the top-level context. Let's find where it's coming from.

Looking at Rack's source code we discover this file sitting in a bin/ directory:

Looks like we're starting the server here. Let's dig into the source code to find out what happens.