We can check if a certain key is present in our environment variables using the has_key()
method in Ruby. This method is invoked on the ENV
class.
ENV.has_key(name)
name
: This is the name of each environment variable that we want to check to see if it is present.
The value returned is a Boolean. If the key exists, then true
is returned. Otherwise, false
is returned.
# create some environment variablesENV["platform"] = "Edpresso"ENV["isBest"] = "true"ENV["user_key"] = "1234skldks"# check if some environment variables existputs ENV.has_key?("user_key") # trueputs ENV.has_key?("isBest") # trueputs ENV.has_key?("platform") # trueputs ENV.has_key?("foo") # falseputs ENV.has_key?("bar") # false
has_key()
method, we check if some environment variable names exist and then print the results to the console.