Ruby code in Gemfile isn't executed

289 Views Asked by At

In my Roda app I have this:

# Gemfile

puts "env is: " + ENV["RACK_ENV"]

if ENV["RACK_ENV"] == "development"
  puts "env dev"
  gem "gem1"
end

This isn't working: the "puts" aren't executed, and the gem "gem1" isn't included either. The "env" is development, I've checked that when a page is loading.

Why not and how to fix it?

2

There are 2 best solutions below

4
On

If what you need is to set gems based on their environment, you use groups in the Gemfile.

You can do some thing like this...

# A sample Gemfile
source 'https://rubygems.org'

gem 'roda'
gem 'haml'

# This is the group for development environment
group :development do
  # gem 'shotgun'
  gem 'gem1'
end

group :test do
  gem 'rspec'
  gem 'capybara'
  gem 'byebug'
end

Coming back to your question...

I assume you haven't set the RACK_ENV variable. I don't develop Roda but I think it is just assuming it is 'development'. So if you want to use RACK_ENV variable in your code, you will need to set it explicitly.

You can do an export RACK_ENV=development to set the env explicitly.

2
On

the "puts" aren't executed, and the gem "gem1" isn't included either.

Actually, this proves that your Ruby code is executed correctly. If you run bundle install or rackup or rails s, the environment variable RACK_ENV is not set, so ENV['RACK_ENV'] is nil.