Trusted answers to developer questions
Trusted Answers to Developer Questions

Related Tags

ruby
env
delete
communitycreator

How to delete an environment variable in Ruby

okwudili onyejiaku

Overview

We use the delete() method to delete an environment variable in Ruby. Environment variables are used to share configuration options between all the programs in our system.

Syntax

ENV.delete(env_name)

Parameter value

env_name: This is the name of the environment variable that we want to delete.

Return value

The delete() method returns the value of the environment variable that is deleted.

Code example

# create some environment variables
ENV["name"] = "OKWUDILI"
ENV["role"] = "DEVELOPER"
ENV["language"] = "RUBY"
ENV["nothing"] = "nothing"

# get all environment variables
puts "All envrionment variables: \n"
for entry in ENV do
  puts "#{entry}"
end

# delete an environment variable
# and print returned value
deletedValue = ENV.delete("nothing")
puts "Deleted value = #{deletedValue}"

# Reprint environment variables
for entry in ENV do
  puts "#{entry}"
end
Deleting an envrionment variable in Ruby

Code explanation

  • Lines 2–5: We create some environment variables.
  • Line 9: We print out the environment variables we have, together with their values, using the for loop. This is done before deleting an environment variable.
  • Line 15: We delete an environment variable called nothing and store the value inside the variable called deletedValue.
  • Line 16: We print out the deleted value of the environment variable.
  • Line 19: We reprint the environment variables once more. Note that the environment variable nothing is no longer in the list.

RELATED TAGS

ruby
env
delete
communitycreator
RELATED COURSES

View all Courses

Keep Exploring