DEV Community

loading...
Cover image for Intro to Ruby Sinatra

Intro to Ruby Sinatra

Alex Merced
My name is Alex Merced of AlexMercedCoder.com. Developer with a focus on full stack web applications.
・1 min read

In Python there is Django, the big batteries included web framework. There there is Flask, the minimalist web framework similar ExpressJS on node. In the Ruby world, along with the batteries included Rails framework, there is the Sinatra web framework. Let's take it for a spin.

Pre-requisites

  • Ruby Installed

Getting Started

  • Create an empty folder

  • Install Sinatra gem install sinatra

  • create a file server.rb with the following

require 'sinatra'

get '/' do
    {"msg": "Hello World"}.to_json
end
Enter fullscreen mode Exit fullscreen mode

Run the file ruby server.rb and checkout localhost:4567.

As you can see getting a Sinatra server started makes Express and Flask look like Rocket science. We literally just installed a gem and wrote 4 lines of code, no virtual environments, no passing environmental variables, no initiating an application object. Just simplicity.

Using URL Params

We can designate parts of the url as params with a colon like in express then access the params via the params hash.

get '/cheese/:type' do
    {"cheese": params["type"]}.to_json
end
Enter fullscreen mode Exit fullscreen mode

Let's create another route. In this case visit /route/this?query=that after updating your code.

get '/route/:param' do
    {"param": params["param"], query: params["query"]}.to_json
end
Enter fullscreen mode Exit fullscreen mode

Learn More

As you can see Sinatra is pretty simple a straightforward. Learn more by reading the documentation.

Discussion (1)

Collapse
yarotheslav profile image
Yaroslav Shmarov

Sinatra is a TOP underrated framework! Really worth diving in!