44

The ENV object in Ruby

 5 years ago
source link: https://www.tuicool.com/articles/hit/7bYnqyu
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.
3QJVvm7.png!web
The ENV object in Ruby

The ENV object in Ruby

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

ENV
ENV
ENV

Feel free to have a look to my new eBook: :book: RUBY OBJECT MODEL :book::gem:

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
  • 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 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.

Have a look to my new eBook : :book: RUBY OBJECT MODEL :book::gem:

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à !

Thank you for taking the time to read this post :-)

Feel free to have a look to my new eBook : :book: RUBY OBJECT MODEL :book::gem:

Feel free to :clap: and share this Medium post if it has been useful for you.

Here is a link to my last medium post: The nil value .


About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK