Trusted answers to developer questions
Trusted Answers to Developer Questions

Related Tags

ruby
communitycreator

How to check if there are no environment variables in Ruby

okwudili onyejiaku

Overview

We can check if there are no environment variables using the empty? property of the ENV class. It returns true if the environment variables are available, otherwise, it returns a false.

Syntax

ENV.empty?

Return values

The value returned is a Boolean. It returns true if there are environment variables available, otherwise, it returns `false.

Code example

# create some environment variables
ENV["G"] = "Google"
ENV["N"] = "Netflix"
ENV["A"] = "Apple"
ENV["A"] = "Amazon"
ENV["M"] = "Meta"
ENV["FOO"] = ""

# check if empty
puts ENV.empty?  # false

# clear environment variables
ENV.clear

# check if empty
puts ENV.empty?  # true
Checking if there are environment variables in Ruby

Code explanation

  • Lines 2 to 7: We create some environment variables.
  • Line 10: We check if there are environment variables present and print the result.
  • Line 13: We clear all environment variables.
  • Line 16: We recheck if the environment variables exist and print the result to the console.

RELATED TAGS

ruby
communitycreator
RELATED COURSES

View all Courses

Keep Exploring