Top 100+ Ruby Sinatra Interview Questions And Answers
Question 1. What Is Sinatra?
Answer :
Sinatra is internet software framework for hastily building programs in Ruby.Sinatra is a site unique language or DSL which means it's miles designed from the floor as much as construct applications with minimal efforts.It is written in Ruby and an opportunity to Ruby web application frameworks which include Ruby on Rails, Merb, Nitro, and Camping.
Question 2. How To Use Sessions In Sinatra?
Answer :
By default, Sessions are disabled in Sinatra.You want to permit them and then use the consultation hash from routes and perspectives. Below code suggests the way to permit, set or get a consultation in Sinatra.
//Enabling Session
enable :sessions
get '/foo' do
// setting consultation value
consultation[:message] = 'Hello World!'
redirect to('/bar')
give up
get '/bar' do
// getting session value
session[:message] # => 'Hello World!'
quit
Perl Scripting Interview Questions
Question three. 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'
elegance 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 handiest the modern request
flash.Now[: notice] = "Thanks for signing up!"
quit
give up
Question 4. How Do I Get The “path” For The Current Page?
Answer :
The request item likely has what you’re looking for:
get '/whats up-world' do
request.Path_info # => '/hiya-international'
request.Fullpath # => '/good day-international?Foo=bar'
request.Url # => 'http://example.Com/hi there-world?Foo=bar'
stop
Perl Scripting Tutorial
Question 5. How Do I Make My Sinatra App Reload On Changes?
Answer :
First off, in-system code reloading in Ruby is difficult and having a solution that works for every scenario is technically not possible.
Which is why we suggest you to do out-of-system reloading.
First you want to put in rerun in case you haven’t already:
$ gem installation rerun
Now in case you begin your Sinatra app like this:
$ ruby app.Rb
All you have to do for reloading is as a substitute do that:
$ rerun 'ruby app.Rb'
If you're for example using rackup, as an alternative do the subsequent:
$ rerun 'rackup'
Python Interview Questions
Question 6. How Do I Use Session-primarily based Flash?
Answer :
Use Rack::Flash.
Question 7. Can I Run Sinatra Under Ruby 1.Nine?
Answer :
Yes. As of Sinatra zero.9.2, Sinatra is completely Ruby 1.9 and Rack 1.Zero well matched. Since 1.1 you do now not ought to address encodings to your personal, until you need to.
Python Tutorial Ruby on Rails Interview Questions
Question 8. How Do I Access Helpers From Within My Views?
Answer :
Call them! Views automatically have access to all helper methods. In reality, Sinatra evaluates routes, views, and helpers in the identical specific item context so they all have access to the same techniques and example variables.
In hey.Rb:
helpers do
def em(text)
"<em>#textual content</em>"
end
stop
get '/howdy' do
@challenge = 'World'
haml :hi there
give up
In perspectives/whats up.Haml:
%p= "Hello " + em(@problem)
Question 9. How Do I Render Partials?
Answer :
Sinatra’s template machine is straightforward sufficient that it can be used for page and fragment stage rendering obligations. The erb and haml methods clearly return a string.
Since Sinatra 1.1, you can use the equal calls for partials you use inside the routes:
<%= erb :partial %>
In variations previous to 1.1, you need to make sure you disable layout rendering as follows:
<%= erb :partial, :layout => false %>
If you're interested in greater robust partials solutions, take a look at out the sinatra-recipes assignment, which has articles on the use of the sinatra-partial gem or imposing your own Rails-style partials.
Ruby Interview Questions
Question 10. Can I Have Multiple Urls Trigger The Same Route/handler?
Answer :
Sure:
["/foo", "/bar", "/baz"].Eachpathdirection do
"You've reached me at #request.Path_info"
give up
give up
Seriously.
Ruby on Rails Tutorial
Question eleven. How Do I Make The Trailing Slash Optional?
Answer :
Put a query mark after it:
get '/foo/bar/?' do
"Hello World"
cease
The direction matches "/foo/bar" and "/foo/bar/".
MVC Framework Interview Questions
Question 12. How Do I Render Templates Nested In Subdirectories?
Answer :
Sinatra apps do not commonly have a very complex file hierarchy under perspectives. First, do not forget whether you really want subdirectories at all. If so,
you could use the views/foo/bar.Haml file as a template with:
get '/' do
haml :'foo/bar'
give up
This is essentially similar to sending #to_sym to the filename and can also be written as:
get '/' do
haml 'foo/bar'.To_sym
give up
Perl Scripting Interview Questions
Question 13. How Do I Escape Html?
Answer :
Use Rack::Utils to your helpers as follows:
helpers do
def h(text)
Rack::Utils.Escape_html(text)
give up
stop
Now you can break out HTML in your templates like this:
<%= h scary_output %>
Thanks to Chris Schneider for the tip!
MVC Framework Tutorial
Question 14. How Do I Automatically Escape Html?
Answer :
Require Erubis and set escape_html to authentic:
require 'erubis'
set :erb, :escape_html => actual
Then, any templates rendered with Erubis will be routinely escaped:
get '/' do
erb :index
stop
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), upload the following on your Rakefile:
namespace :db do
desc "Migrate the database"
project(:migrate => :environment) do
ActiveRecord::Base.Logger = Logger.New(STDOUT)
ActiveRecord::Migration.Verbose = real
ActiveRecord::Migrator.Migrate("db/migrate")
give up
give up
This assumes you've got a challenge called :surroundings which loads your app’s surroundings (requires the proper files, sets up the database connection, etc).
Now you could create a listing called db/migrate and fill on your migrations. I normally call the primary one 001_init.Rb. (I choose the antique sequential method for numbering migrations vs. The datetime technique used given that Rails 2.1, but both will paintings.)
Framework7 Interview Questions
