I have been trying to delve into how sinatra works, and most recently I've been trying to figure out how sinatra starts the server after processing the routes, when it is required at the top of the file. I was looking at this tutorial and they end with an example app looking like this (their version of sinatra is called nancy):
# app.rb
# run with `ruby app.rb`
require "./nancy"
get "/" do
"Hey there!"
end
Rack::Handler::WEBrick.run Nancy::Application, Port: 9292
I am wondering how you are not forced to include that last line in sinatra.
Sinatra does that by defining an
at_exit
callback, see main.rbThis basically says "when the ruby script is done and exits, then run the Sinatra app!"
For more information see the ruby docs for
at_exit
!For serving a sinatra application you just need to execute
ruby app.rb
on shell.app.rb
Then you'll see such output
The tutorial you've been appealing at is not about the actual
sinatra
- there author have built his/her own pseudo-sinatra. By the way, ruby also have a microframework called nancyTo run his/her pseudo-sinatra successfully, you need to follow the tutorial from beggining to end.