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

Overview

The has_value?() method in Ruby checks if a certain value of an environment variable exists. If such a value exists, this methods returns a true Boolean value. Otherwise, it returns a false value.

Syntax

ENV.has_value?("value")

Parameters

value: This is the value whose existence we want to check as an environment variable in our system.

Return value

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

Code example

# Creating some environment variables
ENV["G"] = "Google"
ENV["N"] = "Netflix"
ENV["A"] = "Apple"
ENV["A"] = "Amazon"
ENV["M"] = "Meta"
# Checking if the values exist
puts ENV.has_value?("Google") # true
puts ENV.has_value?("Facebook") # false
puts ENV.has_value?("Meta") # true
puts ENV.has_value?("Blockchain") # false

Explanation

  • Lines 2–6: We create some environment variables.
  • Lines 9–12: We use the has_value?() method to check if some values exist. Then, we print the results to the console.

Free Resources