My second tutorial of the conference is called “A Hat Full of Tricks with Sinatra”. Unlike the morning session on jRuby on Rails, I don’t have any specific plans on how I’d use what I learn here. But the framework looks interesting to me, and I hope to get some good ideas from it even if I never use it for real.
We start by building the minimal “Hello, World” of Rack applications:
run lambda {|env|
[
200,
{‘Content-Length’ => “2”,
‘Content-Type’ => “text/html”},
[“hi”]
]
}
Put that in a file, like “config.ru” and run it with rackup, for example
rackup config.ru -p 3000
The “.ru” extension is for rackup. You can call it “.rb”, but then you have to set more things up in the application because it’s actually running regular Ruby code, instead of being read and interpreted in a special context by rackup.
It’s starting out as a very hands-on tutorial, very basic. But that’s what I want. If it slowly builds from here, I’ll be happy.
Rack runs an application that is given an environment and emits a response. The structure allows you to easily write “middleware”. When Rack’s told to run an application, instead it calls your middleware which can do other things and eventually call your app. This can be chained. It’s a simple and powerful tool.
Okay, now getting into Sinatra. Here’s the core of the application:
require ‘rubygems’
require ‘sinatra’
Huh? Where’s the code? That starts up the framework and gets it all working. If you put those two lines in application and run it (with Ruby) it will start a web server on port 4567. When you open that in a web browser, you’ll get a blurb for Sinatra and suggestions to proceed.
Want to handle a request to a particular path? Here’s code that will respond to requests for the root URL and to /greet:
get ‘/’ do
“Welcome to my Sinatra server”
end
get ‘/greet’ do
“Hello!”
end
Nice and clean. It can also respond to other HTTP verb requests in a similar way.
Now just create a subdirectory of your application directory called “views”, and put a file named “index.erb” in it containing HTML. Change the code that responds to “/” to:
get “/” do
erb :index
end
Now when you run your application and get the root URL, the HTML in that index.erb file gets returned. (ERB is “embedded Ruby” even though we didn’t bother to embed any in that HTML.
I’m really liking how this is set up. Simple always appeals to me, but there’s a lot of power here without messing up that simplicity.
By the way, the sample Sinatra application I’m playing with works just fine under jRuby, too.
I do have one complaint about this tutorial. It’s not well-structured or planned. The speaker seems to think he can just open a terminal window and edit and run source code, and we’ll all have no trouble following. But as soon as one thing is missed, the student has a hard time getting back in sync. He needs to go slower, he needs to describe what he’s about to do and why, he needs to do it, then he needs to tell what he did and why.
He also needed to plan and rehearse every single example he was going to show. Instead, he’s winging a lot of it. The material is trivial to him, but when you’re teaching a class you’ve got to know ahead of time every single tiny glitch you’ll encounter and have to explain.
Another tutorial falls apart in the second half.