@yamilurbina

Web and mobile developer, soon-to-be entrepreneur and startup maker.


Facebook
LinkedIn
Twitter

May 3, 2012 at 6:18pm
Home

Hello World app in Sinatra - Introduction to Rack-Based Apps in Ruby

I’ve been rediscovering Ruby (just Ruby, not Ruby on Rails), playing with rack-based apps and Mongoid, a object-document-mapper for MongoDB. I strongly believe Ruby and MongoDB are a great pair, kind of similar and better than the classic PHP and MySQL duo.

So, this is the first series of posts that show you how to use Sinatra, a rack-based framework to write web applications in a breeze.

Getting started

First you need Ruby 1.9 and Rubygems. How to install them is out of the scope of this tutorial, but I know you’ll find a way to get them working for you.

After you have both, open a command line and type in the following (works on Windows, Linux and OSX):

gem install sinatra

That’s pretty much it the only requirement for now. Later we will add some more gems.

'Hello World' in Sinatra

So, what comes next? That’s right, the ‘hello world’ application that is basic in almost any language.

Write a file named sinatra.rb with the following:

require 'sinatra'

get '/' do
    "Hello World"
end

Save the file.

Now in your command line, browse to the folder where sinatra.rb is, and type in the following:

ruby sinatra.rb

That will start your fresh web application on port 4567 by default. Open your favorite web browser and go to http://0.0.0.0:4567. You will see the Hello world message.

There you go, you wrote your first Sinatra application in less than 2 minutes (according to my calculations).

Wrapping up

As you can see, this is just the beginning. Sinatra is a really powerful tool and I encourage you to read the documentation to learn more about this rack-based solution.

Questions?

You can follow me on twitter @yamilurbina or Google plus for any questions you may have. It’s ok, I’m a cool lad.