Trusted answers to developer questions
Trusted Answers to Developer Questions

Related Tags

ruby
env
pair
communitycreator

What is ENV.each_pair{} 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 each_pair{} method in Ruby is used to yield or give us access to each pair of the name and value of environment variables in a system.

Syntax

ENV.each_pair{|name,value| block}

Parameters

  • name: This represents the name of each environment variable.

  • value: This represents the value of each environmental variable with the name name.

  • block: This gives us access to the key-value pair of an environment variable.

Return value

The value returned is a two-element array containing the environment variable’s name and value.

Code example

# create some environment variables
ENV["1"] = "Meta"
ENV["2"] = "NFT"
ENV["3"] = "Blockchain"
ENV["4"] = "Web 3.0"

# print each pair using the each_pair{} method
ENV.each_pair{|name, value| puts "#{name} = #{value}"}
Getting the environment variable name and value in pair

Code explanation

  • Lines 2 to 5: We create some environment variables.
  • Line 8: We use the each_pair{} method of the ENV class to get access to each environment variable’s name and value. Lastly, we print the pairs to the console.

RELATED TAGS

ruby
env
pair
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