Trusted answers to developer questions
Trusted Answers to Developer Questions

Related Tags

ruby
communitycreator

How to check if all the environment variables are empty in Ruby

okwudili onyejiaku

Overview

We use the ENV class to store environment variables. These environment variables can be secrets and configurations we want to keep private. We can check if all these variables are empty by using the empty? property.

Syntax

ENV.empty?

Return value

The empty? function returns true when there are no environment variables. Otherwise, it returns false.

Example

The following piece of code completes the picture and shows the example with its output:

# create some environment variables
ENV["payment_key"] = "323selfdsd34"
ENV["authorization_key"] = "123456788"

# check if ENV is empty 
puts ENV.empty? # false

# print all environment variables
ENV.each_pair{|name, value| puts "#{name} = #{value}"}

# clear all variables
ENV.clear

# check if ENV is empty
puts ENV.empty? # true
How to check if all environment variables are empty

Explanation

  • Line 2 and 3: We create two environment variables on our own.
  • Line 6: We check if ENV is empty.
  • Line 9: We use the each_pair{} method that gives us access to each environment variable name and value.
  • Line 12: We clear all environment variables using the clear method.
  • Line 15: We check if ENV is empty once more.

RELATED TAGS

ruby
communitycreator
RELATED COURSES

View all Courses

Keep Exploring