What is the ENV.include?() method in Ruby?

Overview

The ENV.include?() method in Ruby lets us check if a certain environment name is present in our environment variables. It returns a Boolean value.

Syntax

ENV.include?(name)
Syntax to check if a certain environment variable exists

Parameters

name: This is the name of the environment variable whose existence we want to check.

Return value

This method returns a Boolean value. If the environment variable exists, it returns a true value. Otherwise, it returns a false value.

Code example

# Creating some environment variables
ENV["name"] = "Ruby"
ENV["platform"] = "Edpresso"
ENV["user_secret"] = "23423lsdfksld23"
# Checking if some keys exist
puts ENV.include?("name")
puts ENV.include?("platform")
puts ENV.include?("admin_secret")

Explanation

  • Lines 2–4: We create some environment variables.
  • Lines 7–9: We check if the environment variables exist. Then, we print the values to the console.

Free Resources