Sinatra Basics : The Current Environment
To learn about the current environment in a Sinatra app.
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 end
will display 'development' in development.
will display 'test'.
to revert back to 'development'.
The quirky thing about Sinatra is if you have a file with:
It will start running a server on the terminal.
How to make Sinatra library programmable? Here is the trick to not let Sinatra run.
require 'sinatra' puts Sinatra::Application.environment at_exit do exit end
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.