"require if" in Ruby

Here is what I have in my rack application

#rb file require 'pry' class ..... #GemFile group :development do gem "pry" gem "pry-nav" end 

Of course, this causes a mistake in production. How to make a kind of "demand if"?

+4
source share
3 answers
 require 'pry' if ENV['RACK_ENV'] == 'development' 
+13
source

Maybe you can embed it in an if block

according to docs, Sinatra provides an environment variable http://www.sinatrarb.com/intro#Environments

 if development? require 'pry' end 

where you need to use it.

it may not be the exact solution that you can find is just a wild guess

+3
source

I suggest writing such a method in Object or Kernel in your application:

 def require_pry require 'pry' if ENV['RACK_ENV'] == 'development' end 

After that, you can call require_pry if you need it in the code. But I doubt why the Bundler cannot be handled by the Bundler. Rquire will require all the gems needed for the environment.

-1
source

All Articles