1

I am not writing in Rails. It is just ruby.

But I have a dev environment that has it's own development group in the Gemfile.

But I don't use them in production on Iron.io.

In particular, I use "log_buddy" and have lots of d {var} statements throughout.

And I use pry which has a require pry and require-debug statement.

These statements create errors in the case of pry and duplicate logging in the case of log_buddy when the code runs in production.

How do I make a distinction between the two environments?

I have read about dotenv and some other gem, but didn't quite understand how it would work in my scenario.

miken32
  • 42,008
  • 16
  • 111
  • 154
Satchel
  • 16,414
  • 23
  • 106
  • 192

2 Answers2

1

If you have just yes/no scenario for dev, dotenv family is an overkill. I would go with surrounding dev requirements with:

if ENV['DEV']
  require 'pry'
  ...
end

and then run development scenarios as:

DEV=true bundle exec ...

Since DEV env variable is not defined on your prod server, nothing will be included there.

Init for log_buddy might look like:

LogBuddy.init(ENV['DEV'] ? {:logger => Logger.new('my_log.log')} : nil)
Aleksei Matiushkin
  • 119,336
  • 10
  • 100
  • 160
  • Thanks, what does the Init do? I would like it if there's no DEV environment to pass the following: `LogBuddy.init :disabled => true` – Satchel May 17 '15 at 16:18
  • would I run the web app this way: `DEV = true bundle exec shotgun -o 0.0.0.0 web....rb` (everything from `bundle` on is how I run the app currently in dev mode. – Satchel May 17 '15 at 16:19
1

Not using Rails does not prevent you from using Bundler groups:

# These gems are in the :default group
gem 'nokogiri'
gem 'sinatra'

gem 'wirble', :group => :development

group :test do
  gem 'faker'
  gem 'rspec'
end

group :test, :development do
  gem 'capybara'
  gem 'rspec-rails'
end

gem 'cucumber', :group => [:cucumber, :test]

Then you have to get the environment name in any way you deem reasonable:

bundler_env = whatever # could be ENV['ENVIRONMENT'], for instance
bundler_env ||= :production # Specify a fallback if none specified

And once you're done, require the gems:

Bundler.require(:default, bundler_env)
D-side
  • 9,150
  • 3
  • 28
  • 44
  • how would this take care of lines of code that rely on the gems, like log_buddy's `d`? If I could set the environment variable for log_buddy it could stop output – Satchel May 17 '15 at 16:16
  • *When you occasionally want to disable LogBuddy (but you don't want to have to remove all your debug statements), you can pass the :disabled option into init's options hash* ([ref](https://github.com/relevance/log_buddy)) – D-side May 17 '15 at 16:26
  • And you shouldn't actually have any debug statements in your code except for `debug`-level logging. That's why `rubocop` complains about any `binding.pry` it sees. – D-side May 17 '15 at 16:29
  • how do I pas the disabled option when it is in production? Thanks.k – Satchel May 18 '15 at 05:36
  • @Angela example of setting encironment name is right above. Use an `if` on that. – D-side May 18 '15 at 06:27
  • So 'if bundler_env == develop then' and so as long as I specify bundler env when running that does it? And is it by saying 'env = true bundler exec shotgun -o .....'? – Satchel May 19 '15 at 14:17
  • @Angela `bunlder` only governs which gems to require, any environment-specific statements are still up to you. The comment above suggests launching via `ENVIRONMENT=test bundle exec whatever ...`, that would make the value `"test"` available in `ENV['ENVIRONMENT']`, so you can make any choices. Rails also uses the environment name to guess which initializer file to load, you could do that too. – D-side May 19 '15 at 14:27
  • okay so since production would not have an ENVIRONMENT = test then I can put the logic in my app....I think I get it now, thanks. – Satchel May 22 '15 at 16:42
  • I tried to pass the ENVIRONMENT variabel per above and it gives me an error: `site git:(master) ✗ ENVIRONMENT = test bundle exec shotgun -o 0.0.0.0 web_post.rb zsh: command not found: ENVIRONMENT` – Satchel May 31 '15 at 02:41
  • @Angela no spaces near the first =. – D-side May 31 '15 at 09:26
  • is `Bundler.require(:default, bundler_env)` what I what put in the actual ruby files instead of the list of require 'gem'? – Satchel Jul 08 '15 at 00:33