Sinatra Basics : The Current Environment


To learn about the current environment in a Sinatra app.


Step 1

Here is a rake task to display the current environment in a Sinatra app.

desc 'Show the current environment'
task :env do
  require 'sinatra'
  puts Sinatra::Application.environment

Step 2

$rake env

will display 'development' in development.

Step 3

$export RACK_ENV='test'
$rake env

will display 'test'.

Step 4

$unset RACK_ENV

to revert back to 'development'.

Step 5

The quirky thing about Sinatra is if you have a file with:

require 'sinatra'

It will start running a server on the terminal.

Step 6

How to make Sinatra library programmable? Here is the trick to not let Sinatra run.

require 'sinatra'
puts Sinatra::Application.environment

at_exit do

Since Sinatra uses at_exit hook to start a server, we can over-ride it so that we exit the program once we are done with our task.


We were able to print the current Sinatra environment in a rake task without running a server. How does the rake task prevent Sinatra from running the server?


In this article you learned about Sinatra environment and how to write a script that does not run a server when it is not required.

Related Articles

Ace the Technical Interview

  • Easily find the gaps in your knowledge
  • Get customized lessons based on where you are
  • Take consistent action everyday
  • Builtin accountability to keep you on track
  • You will solve bigger problems over time
  • Get the job of your dreams

Take the 30 Day Coding Skills Challenge

Gain confidence to attend the interview

No spam ever. Unsubscribe anytime.