The delete_if{}
method is used to delete an environment variable that returns “true” when passed to a given block.
ENV.delete_if{|name,value|condition}
name
: This represents the name of each environment value.
value
: This represents the value of the environment variable name
.
condition
: This takes the environment variable name
or value
and returns “true” or “false” depending on what condition was specified.
The value returned is ENV
.
# create some environment variablesENV["G"] = "Google"ENV["N"] = "Netflix"ENV["A"] = "Apple"ENV["A"] = "Amazon"ENV["M"] = "Meta"ENV["FOO"] = ""# delete some environment variables# based on a conditionENV.delete_if{|name, value| name.start_with?('A')}# delete variables that starts with "A"# print environment variablesputs ENV.values
"A"
."A"
were deleted.