ENV variables are strings

This is part of the Semicolon&Sons Code Diary - consisting of lessons learned on the job. You're in the bash category.

Last Updated: 2024-03-02

I wanted my tests to take a special action when running on my CI server. Seeing that they set the env variable 'CI' to 'true' I wrote the following code:

continuous_integration_env = ENV['CI']
if Rails.env.production? || (Rails.env.test? && continuous_integration_env == true)
  run_me
end

This, of course, didn't work. Why? Because "true" != true

Similarly I once had the following code for passing Ruby-land variables to JavaScript:

Webpacker::Compiler.env['FORCE_COOKIE_NOTICE'] = true

It failed:

ActionView::Template::Error (no implicit conversion of true into String):

The issue again was that a string was expected as an env variable. An example fix:

Webpacker::Compiler.env['FORCE_COOKIE_NOTICE'] = "true"

Lesson

Always treat ENV variables as simple strings, no matter what.