The member?()
method of the ENV
class in Ruby tells us if there is an environment variable with the given name. The name
is passed as a parameter to the member?()
method.
ENV.member?(name)
This method takes the parameter, name
, which represents the name of the environment variable for which we want to check.
It returns a boolean value. It returns true
if an environment variable with such name exists. Otherwise, it returns false
.
# clear default environment variablesENV.clear# create some environment variablesENV["secret_name"] = "secret"ENV["secret_token"] = "123"ENV["private_key"] = "server123"ENV["foo"] = "1"ENV["bar"] = "0"# use the key() method in driver codeenv_name = ["secret_name", "secret", "secret_token", "bar"]for i in 0..3 doprint(env_name[i], ": " , ENV.member?(env_name[i]), "\n")end
member?()
, along with some name
as parameters. We check if some variables exists. Next, we print the results.