The ENV object in Ruby

In this article we’re going to explore the following topics:

the ENV object

object ENV and the standard library functions

and the standard library functions ENV behind the scene

Before to start

I’m thrilled to share with you our latest project: Fun Facts about Ruby — Volume 1

Please feel free to spread the word and share this post! 🙏

Thank you for your time!

The ENV Object

ENV provides an API to manipulate the environment variables.

By acting like a Hash , it provides a set of methods to add, change, delete and access environment variables that is definitely Ruby-friendly

irb-001> ENV['AN_ENV_VARIABLE'] = 'cool!'

=> "cool!"

irb-002> ENV['AN_ENV_VARIABLE']

=> "cool!"

irb-003> ENV['AN_ENV_VARIABLE'] = 'great!'

=> "great!"

irb-004> ENV.delete('AN_ENV_VARIABLE')

=> "great!"

irb-005> ENV.fetch('AN_ENV_VARIABLE', 'a default value')

=> "a default value"

In the above example we can see a brief use of the API:

001- we set the 'cool!' value to the AN_ENV_VARIABLE environment variable using the ENV.[]= method

value to the environment variable using the method 002- then we access the value of AN_ENV_VARIABLE using the ENV.[] method

using the method 003- we modify the value of AN_ENV_VARIABLE to 'great!' using the ENV.[]= method

to using the method 004- we delete the AN_ENV_VARIABLE environment variable using the ENV.delete method

environment variable using the method 005- we try to access the AN_ENV_VARIABLE and we provide a default value if it doesn’t exist using the ENV.fetch method

That’s cool ! But where are the system environment variables?

ENV and the standard library functions

The ENV object relies on the C standard library functions to manage the environment variables.

For example, when you call the ENV.[] method then Ruby calls the appropriate C standard library function depending of your OS — getenv(3) for unix-like OS for example — to fetch the appropriate environment variable.

This system is efficient and relies on a strong standard library.

Furthermore, only the manipulated environment variables at runtime are accessed. There is no environment variables preloaded in memory at Ruby program startup.

So, now let’s have a look to how ENV is implemented behind the scene.

ENV behind the scene

ENV is a hash-like object. This means that it’s an instance of Object and that it has a bunch of methods similar to an instance of Hash .

irb> ENV.class

=> Object

Behind the scene, the ENV object recodes the hash-like methods (as ENV[ENVIRONMENT_VARIABLE] ) in order to use the *env(3) C functions family. So the ENV object is just a Ruby wrapper on C functions that are in charge of managing the environment variables

Furthermore, the ENV object extends the Enumerable module but overrides a bunch of methods of this module as each and each_pair for example

irb> ENV.singleton_class.included_modules

=> [Enumerable, Kernel]

To recap, ENV is an “enumerable” instance of Object stored in a global constant.

Feel free to browse the hash.c file for further information.

Voilà !

May I have your attention please 🎤🎤

Feel free to subscribe here: www.rubycademy.com