Trusted answers to developer questions
Trusted Answers to Developer Questions

Related Tags

ruby
env
member
communitycreator

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

okwudili onyejiaku

Grokking Modern System Design Interview for Engineers & Managers

Ace your System Design Interview and take your career to the next level. Learn to handle the design of applications like Netflix, Quora, Facebook, Uber, and many more in a 45-min interview. Learn the RESHADED framework for architecting web-scale applications by determining requirements, constraints, and assumptions before diving into a step-by-step design process.

Overview

The member?() method of the ENV class in Ruby tells us if there is an environment variable with the given name. The name is passed as a parameter to the member?() method.

Syntax

ENV.member?(name)
Syntax for the member?() method

Parameters

This method takes the parameter, name, which represents the name of the environment variable for which we want to check.

Return value

It returns a boolean value. It returns true if an environment variable with such name exists. Otherwise, it returns false.

Example

# clear default environment variables
ENV.clear
# create some environment variables
ENV["secret_name"] = "secret"
ENV["secret_token"] = "123"
ENV["private_key"] = "server123"
ENV["foo"] = "1"
ENV["bar"] = "0"
# use the key() method in driver code
env_name = ["secret_name", "secret", "secret_token", "bar"]
for i in 0..3 do
print(env_name[i], ": " , ENV.member?(env_name[i]), "\n")
end
Check if an environment variable name exists using member?() method

Explanation

  • Line 2: We clear all present environment variables.
  • Line 5–9: We create some new environment variables.
  • Line 14: We use the member?(), along with some name as parameters. We check if some variables exists. Next, we print the results.

RELATED TAGS

ruby
env
member
communitycreator

Grokking Modern System Design Interview for Engineers & Managers

Ace your System Design Interview and take your career to the next level. Learn to handle the design of applications like Netflix, Quora, Facebook, Uber, and many more in a 45-min interview. Learn the RESHADED framework for architecting web-scale applications by determining requirements, constraints, and assumptions before diving into a step-by-step design process.

Keep Exploring