Over a million developers have joined DZone.
{{announcement.body}}
{{announcement.title}}

Ruby: Checking for environment variables in a script

DZone's Guide to

Ruby: Checking for environment variables in a script

·
Free Resource

I’ve been working on a Ruby script to allow us to automate part of our Solr data setup and part of the task was to check that some environment variables were set and throw an exception if not.

I got a bit stuck initially trying to work out how to return a message showing only the missing environment variables but it turned out to be pretty simple when I came back to it a couple of hours later.

So for my future reference than anything else, this is what I ended up with:

  variables = %w{VARIABLE_1 VARIABLE_2}
missing = variables.find_all { |v| ENV[v] == nil }
unless missing.empty?
raise "The following variables are missing and are needed to run this script: #{missing.join(', ')}."
end

I recently came across ‘%w’ which creates a string array out of the values that we specify.

In this case we therefore end up with…

> %w{VARIABLE_1 VARIABLE_2}
["VARIABLE_1", "VARIABLE_2"]

…which I think is pretty neat!

The other neat method is ‘join’ on line 4 which concatenates all the elements of an array while putting the provided separator in between each element.

In this case if we had neither of the variables specified we’d end up with the following:

> ["VARIABLE_1", "VARIABLE_2"].join(', ')
"VARIABLE_1, VARIABLE2"

 

Topics:

Published at DZone with permission of Mark Needham, DZone MVB. See the original article here.

Opinions expressed by DZone contributors are their own.

THE DZONE NEWSLETTER

Dev Resources & Solutions Straight to Your Inbox

Thanks for subscribing!

Awesome! Check your inbox to verify your email so you can start receiving the latest in tech news and resources.

X

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}