Photo by Hamdhulla Shakeeb on Unsplash

The ENV object in Ruby

Tech - RubyCademy
RubyCademy
Published in
3 min readJul 19, 2018

--

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

  • the ENV object
  • ENV and the standard library functions
  • ENV behind the scene

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 are 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
  • 002- then we access the value of AN_ENV_VARIABLE using the ENV.[] method
  • 003- we modify the value of AN_ENV_VARIABLE to 'great!' using the ENV.[]= method
  • 004- we delete the AN_ENV_VARIABLE environment variable using the ENV.delete 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 on 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 are no environment variables preloaded in memory at Ruby program startup.

So, now let’s have a look at 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 ENV 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.

RubyCademy’s Newsletter

In the RubyCademy newsletter, I share what I’m learning about Ruby & Rails, including helpful tips, code insights, and exclusive Ruby Cards with detailed explanations.

If it sounds helpful, feel free to subscribe for free:

Thank you for taking the time to read this message!

--

--