[MacRuby-devel] ControlTower/Rack question

Joshua Ballanco joshua.ballanco at apple.com
Wed Aug 25 14:28:40 PDT 2010


Hi Alex,

This is as good a place as any for the time being.

So, the way that Rack works is that it takes an app *object* (not a class) and calls the "call" method on that object each time a request comes in. To facilitate the use of middleware and the combination of multiple Rack "app objects" in one server process, Rack also comes with a builder which uses a minimal DSL to collect these objects. What you're using is that builder DSL. So, to break it down:

> map '/foo' do
# The 'map' keyword in the DSL tells Rack that any time a request that starts with "/foo" comes in, it should use the app contained inside this block (which Rack finds by #call'ing the block passed to 'map' at start up.
> 	$text = "foo"
> 	print $text
# This is setup that will occur when Rack first #call's the block
>     run Proc.new {|env| [200, {"Content-Type" => "text/html"}, $text ] }
# The 'run' keyword tells Rack that the argument following 'run' is the "app object". In this case, you've provided a Proc object. Each HTTP request that comes in, with a path starting with "/foo", will #call this Proc object. Put your "print $text" line inside the block passed to Proc.new, and you'll see it getting called each time.
> end

If you're curious about the Rack::Builder DSL, the entire thing is only 55 lines. You can find it in the ControlTower project in lib/control_tower/vendor/rack/builder.rb

Cheers,

Josh
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.macosforge.org/pipermail/macruby-devel/attachments/20100825/96acfc74/attachment.html>


More information about the MacRuby-devel mailing list