Running with Ruby

Tag: environment

Envlogic – Ruby gem to have a Rails.env like environment behaviour in your non Rails Ruby applications

Envlogic is a simple gem that will help you manage your Ruby application environment. You can use it instead of a really popular solution like this one:

class App
  def self.env
    ENV['APP_ENV'] || ENV['RACK_ENV'] || 'development'

How does it work?

Well it uses Rails String Inquirer and a bit of magic to resolve the environment key name. It will try to figure out the ENV key on its own, and if it fails it will fallback to development. To use it, just extend your class/module with Envlogic module:

Class MyApp
  extend Envlogic

MyApp.env #=> 'development'
MyApp.env.production? #=> false
MyApp.env.development? #=> true

ENV key name resolving

Envlogic will try to guess the key n few steps:

  • First it will try to guess the key based on the root directory name of your app. If it is called my_app – the key will be MY_APP_ENV (ENV[‘MY_APP_ENV’])
  • If this fails, it will try to resolve the class/module name in which it is – MyApp will be converted to MY_APP_ENV. It is worth pointing out, that this will also work for namespaced elements, so key for My::SuperApp will be equal to MY_SUPER_APP_ENV
  • It will try RACK_ENV as well
  • And finally will fallback to development

Where can I use it?

Anywhere outside of Rails app (it has a mechanism like this already built it). Just include this gem in your Gemfile and extend your class with it:

gem 'envlogic'

Can I safely replace my string solution with this one?

Yes. The env object (Envlogic::Env) behaves mostly like a typical string, so if you used to compare the env like this:

MyApp.env == 'production'

it will keep working as nothing would happen.

Few simple examples

Based on the app class name:

class MyApp
  extend Envlogic

ENV['MY_APP_ENV'] = 'production'

MyApp.env.production? #=> true
MyApp.env.development? #=> false
MyApp.env.test? #=> false

Based on the app dir name (/home/apps/super_app):

class App
  extend Envlogic

ENV['SUPER_APP_ENV'] = 'production'

App.env.production? #=> true
App.env.development? #=> false
App.env.test? #=> false

Based on the namespaced class name:

class Ux::App
  extend Envlogic

ENV['UX_APP_ENV'] = 'production'

Ux::App.env.production? #=> true
Ux::App::App.env.development? #=> false
Ux::App.env.test? #=> false

Phusion Passenger and environment variables with Ruby (Rake) applications

Sometimes we get software where all (or most of) settings are passed into application server via environment variables. But Passenger is not executed directly. So how do we pass ENV variables into it?

We need to create a little “wrapper” around Ruby, so it will be executed with our environment variables. Here’s a little example (

export  MONGODB_HOST= \
export  MONGODB_PORT=27017 \
export MONGODB_DBNAME=openaudit \
export REDIS_HOST= \
export REDIS_PORT=6379 \
export REDIS_DB=10 \

exec "/usr/local/bin/ruby" "$@"

Script must be executable with chmod +x and should be placed in /usr/local/bin/ dir.

There is one more thing to do: tell Passenger where the wrapper is (vhost example):

    <VirtualHost *:3001>
      RackEnv production
      PassengerMaxPoolSize 30
      PassengerUseGlobalQueue on
      PassengerRuby /usr/local/bin/ruby-wrapper
      DocumentRoot /home/smhtng/app/current/public/
      PassengerUser openaudit   
      <Directory /home/smhtng/app/current/public/>
         AllowOverride all             
         Options -MultiViews            

The “magic” happens here:

 PassengerRuby /usr/local/bin/ruby-wrapper

Copyright © 2018 Running with Ruby

Theme by Anders NorenUp ↑