Ruby Sinatra Interview Questions & Answers

Ruby Sinatra Interview Questions

Are you preparing for Ruby Sinatra interview? Ruby Sinatra is the software application. It is available for free in internet as it is open source. It is written in Ruby, a domain specific language. It is brought into market as an alternative to Metro, Nitro, Ruby on Rails. Candidates are supposed to have basic knowledge on the functionality of it. Many companies are in need of such people. There is less number of people who are skilled in Ruby Sinatra. There is huge demand for this technology in the market. Opportunities are everywhere for this technology. Good knowledge on how the process is done will fetch you the job. Wisdomjobs framed Ruby Sinatra interview questions and answers to make it easier for your interview preparation.

Ruby Sinatra Interview Questions And Answers

Ruby Sinatra Interview Questions
    1. Question 1. What Is Sinatra?

      Answer :

      Sinatra is web application framework for rapidly building applications in Ruby.Sinatra is a domain specific language or DSL which means it is designed from the ground up to build applications with minimal efforts.It is written in Ruby and an alternative to Ruby web application frameworks such as Ruby on Rails, Merb, Nitro, and Camping.

    2. Question 2. How To Use Sessions In Sinatra?

      Answer :

      By default, Sessions are disabled in Sinatra.You need to enable them and then use the session hash from routes and views. Below code shows how to enable, set or get a session in Sinatra.

      //Enabling Session

      enable :sessions

      get '/foo' do

        // setting session value

        session[:message] = 'Hello World!'

        redirect to('/bar')


      get '/bar' do

       // getting session value

        session[:message]   # => 'Hello World!'


    3. Question 3. How Do You Flash A Session Message In Sinatra ?

      Answer :

      Rack:: Flash is used to flash a message in Sinatra.

      Example Usage :

      require 'sinatra/base'

      require 'rack-flash'

      class MyApp < Sinatra::Base

        enable :sessions

        use Rack::Flash

        post '/set-flash' do

          # Set a flash entry

          flash[: notice] = "Thanks for signing up!"

          # Get a flash entry

          flash[:notice] # => "Thanks for signing up!"

          # Set a flash entry for only the current request

[: notice] = "Thanks for signing up!"



    4. Question 4. How Do I Get The “route” For The Current Page?

      Answer :

      The request object probably has what you’re looking for:

      get '/hello-world' do

        request.path_info   # => '/hello-world'

        request.fullpath    # => '/hello-world?foo=bar'

        request.url         # => ''


    5. Question 5. How Do I Make My Sinatra App Reload On Changes?

      Answer :

      First off, in-process code reloading in Ruby is hard and having a solution that works for every scenario is technically impossible.

      Which is why we recommend you to do out-of-process reloading.

      First you need to install rerun if you haven’t already:

      $ gem install rerun

      Now if you start your Sinatra app like this:

      $ ruby app.rb

      All you have to do for reloading is instead do this:

      $ rerun 'ruby app.rb'

      If you are for instance using rackup, instead do the following:

      $ rerun 'rackup'

    6. Question 6. How Do I Use Session-based Flash?

      Answer :

      Use Rack::Flash.

    7. Question 7. Can I Run Sinatra Under Ruby 1.9?

      Answer :

      Yes. As of Sinatra 0.9.2, Sinatra is fully Ruby 1.9 and Rack 1.0 compatible. Since 1.1 you do not have to deal with encodings on your own, unless you want to.

    8. Question 8. How Do I Access Helpers From Within My Views?

      Answer :

      Call them! Views automatically have access to all helper methods. In fact, Sinatra evaluates routes, views, and helpers within the same exact object context so they all have access to the same methods and instance variables.

      In hello.rb:

      helpers do

        def em(text)




      get '/hello' do

        @subject = 'World'

        haml :hello


      In views/hello.haml:

      %p= "Hello " + em(@subject)

    9. Question 9. How Do I Render Partials?

      Answer :

      Sinatra’s template system is simple enough that it can be used for page and fragment level rendering tasks. The erb and haml methods simply return a string.

      Since Sinatra 1.1, you can use the same calls for partials you use in the routes:

      <%= erb :partial %>

      In versions prior to 1.1, you need to make sure you disable layout rendering as follows:

      <%= erb :partial, :layout => false %>

      If you are interested in more robust partials solutions, check out the sinatra-recipes project, which has articles on using the sinatra-partial gem or implementing your own Rails-style partials.

    10. Question 10. Can I Have Multiple Urls Trigger The Same Route/handler?

      Answer :


      ["/foo", "/bar", "/baz"].each do |path|

        get path do

          "You've reached me at #{request.path_info}"




    11. Question 11. How Do I Make The Trailing Slash Optional?

      Answer :

      Put a question mark after it:

      get '/foo/bar/?' do

        "Hello World"


      The route matches "/foo/bar" and "/foo/bar/".

    12. Question 12. How Do I Render Templates Nested In Subdirectories?

      Answer :

      Sinatra apps do not typically have a very complex file hierarchy under views. First, consider whether you really need subdirectories at all. If so,

      you can use the views/foo/bar.haml file as a template with:

      get '/' do

        haml :'foo/bar'


      This is basically the same as sending #to_sym to the filename and can also be written as:

      get '/' do

        haml 'foo/bar'.to_sym


    13. Question 13. How Do I Escape Html?

      Answer :

      Use Rack::Utils in your helpers as follows:

      helpers do

        def h(text)




      Now you can escape HTML in your templates like this:

      <%= h scary_output %>

      Thanks to Chris Schneider for the tip!

    14. Question 14. How Do I Automatically Escape Html?

      Answer :

      Require Erubis and set escape_html to true:

      require 'erubis'

      set :erb, :escape_html => true

      Then, any templates rendered with Erubis will be automatically escaped:

      get '/' do

        erb :index


    15. Question 15. How Do I Use Activerecord Migrations?

      Answer :

      From Adam Wiggins’s blog:

      To use ActiveRecord migrations with Sinatra (or other non-Rails project), add the following to your Rakefile:

      namespace :db do

        desc "Migrate the database"

        task(:migrate => :environment) do

          ActiveRecord::Base.logger =

          ActiveRecord::Migration.verbose = true




      This assumes you have a task called :environment which loads your app’s environment (requires the right files, sets up the database connection, etc).

      Now you can create a directory called db/migrate and fill in your migrations. I usually call the first one 001_init.rb. (I prefer the old sequential method for numbering migrations vs. the datetime method used since Rails 2.1, but either will work.)

Popular Interview Questions

All Interview Questions

All Practice Tests

All rights reserved © 2020 Wisdom IT Services India Pvt. Ltd Protection Status

Ruby on Rails Tutorial