From PHP to Ruby: What is a Rack-based app?

I want to start this blog series called “From PHP to Ruby”, which are intended to be small articles on how different is one from another and such. I hope somebody can learn something ;)

If you’re new in Ruby world and came from, not just PHP, but every language, you may notice some technical terms around it. In the beginning, I was pretty confused about “Rack-based” stuff. So I’m gonna try to explain what I’ve learned from it.

In the beginning, the way every application was exposed to the web was through CGI. So I could write a C application and using a webserver with CGI support, it could communicate with web.

However, this method has many cons: each framework or webapp has to write the basic web flow structure: Request, Response, Params, Cookies, Environment and so on. And therefore, many projects often wrote the code that solved the same problem: parsing the Http request. Pretty much what PHP’s Symfony calls HttpFoundation. Today PHP-FIG has a standard for this so called Message Interface, PSR7.

So, someday Ryan Allen came up with the idea of creating a simple API interface that sits between the webserver and the webapplication (thus it’s a ‘middleware’) and called it Rack.

Differently than PHP’s similar projects that are different for almost every framework, this middleware became very popular and widely supported, so today almost every Ruby webserver and pretty every Ruby web framework also supports it.

Writing a simple Rack application it’s really easy, all you have to write is a a lambda like so:

run lambda { [200, {"Content-Type" => "text/html"}, "Hello Rack!"] }

That’s pretty much it! :)