Skip to content

Instantly share code, notes, and snippets.

Last active Feb 19, 2020
What would you like to do?
Sinatra on Cloud9

Cloud 9 - Sinatra

Setting Up Cloud9

  • Go to and log in
  • Create a new workspace
    • Workspace name = hello-world
    • Clone from Git or Mercurial URL =
    • (everything else at defaults)

In Cloud9

Check out the files you have in this folder, see what they have.

In the terminal (at the bottom where it says bash) type:

  • bundle install
    • this will install the ruby gems listed in the Gemfile that you need to have sinatra run
  • ruby hello_world.rb -o $IP -p $PORT
    • this will run the file hello_world.rb, and tell it to make the site available online at a location.
    • Cloud 9 has wired things up so $IP and $PORT contains what it should on their server.
  • In the menu bar, click Preview => Preview Running Application and a browser pane will appear.
  • After each change you make to our files:
    • save the file(s)
    • refresh the browser pane


  • Make a folder views
  • Add the file views/index.erb (a file named index.erb in the folder views/) with the content
<h1>This is my index.erb file contents oh my.</h1>
  • modify hello_world.rb to use index.erb instead of returning a string.
    • Within the get '/' do: change "Hello World! Welcome to Ruby!" to erb :index.

Pass Params

  • Modify your hello_world.rb get '/' do to include:
  get '/' do 
    num = params['num'].to_i
    @result = num+10
    status 200
    erb :index  
  • Now in your index.erb add this to show the result
  <%= "The result if #{@result}" %>

If we add ?num=10 to the url (mine looks like, the result should 20

  • Now try to modify the code in hello_world.rb's get '/' route to work with the fizzbuzz code we did earlier.

Pair Exercise

Get your FizzBuzz code from earlier to work on Sinatra.

  • Goal: You go to and it displays on the page whether it is fizz, buzz, etc. For example 10: Fizz.
  • You'll want to pass the result fizz or buzz to the template by assigning it to the variable @result.
  • You'll want to use a function def fizzbuzz... that returns a value
source ''
gem 'sinatra'
gem 'sinatra-contrib'
require 'sinatra'
require 'sinatra/reloader' if development?
# Commands to run
# Things to try
# * have the page display "Howdy!"
# * have the page display some HTML (paste some HTML in the string)
# Bonus
# * have the page display the current time (google "ruby current time")
# * have the page display the current time, formatted in a nicer way. (google "ruby strftime" which stands for string-format-time)
get '/' do
"Hello World! Welcome to Ruby!"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment