Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What object does ENV return in Ruby?

Tags:

ruby

There's an ENV variable we all know. It returns an Object. But the return value of ENV is visually similar to a Hash. But it's really not.

For example:

> ENV
=> {"SHELL"=>"/bin/bash", "SESSION_MANAGER"=>"local/archlinux:@/tmp/.ICE-unix/613,unix/archlinux:/tmp/.ICE-unix/613", "COLORTERM"=>"truecolor", "XDG_CONFIG_DIRS"=>"/etc/xdg", "XDG_MENU_PREFIX"=>"xfce-", "SSH_AUTH_SOCK"=>"/tmp/ssh-Al0pdO1R5970/agent.622", "DESKTOP_SESSION"=>"Xfce Session", "SSH_AGENT_PID"=>"623", "GTK_MODULES"=>"canberra-gtk-module:canberra-gtk-module", "XDG_SEAT"=>"seat0", "PWD"=>"/home/sourav", "LOGNAME"=>"sourav", "XDG_SESSION_TYPE"=>"x11", "XAUTHORITY"=>"/home/sourav/.Xauthority", "HOME"=>"/home/sourav", "LANG"=>"en_GB.UTF-8", "XDG_CURRENT_DESKTOP"=>"XFCE", "VTE_VERSION"=>"5603", "INVOCATION_ID"=>"6d4dc7a91cc141e691436cb850e18f8d", "GLADE_CATALOG_PATH"=>":", "XDG_SESSION_CLASS"=>"user", "TERM"=>"xterm-256color", "USER"=>"sourav", "DISPLAY"=>":0.0", "SHLVL"=>"2", "XDG_VTNR"=>"1", "XDG_SESSION_ID"=>"1", "TILIX_ID"=>"f2480263-263e-408f-be36-8105e71256a6", "MOZ_PLUGIN_PATH"=>"/usr/lib/mozilla/plugins", "GLADE_MODULE_PATH"=>":", "XDG_RUNTIME_DIR"=>"/run/user/1000", "GLADE_PIXMAP_PATH"=>":", "JOURNAL_STREAM"=>"9:25041", "XDG_DATA_DIRS"=>"/usr/local/share:/usr/share", "PATH"=>"/usr/local/sbin:/usr/local/bin:/usr/bin:/usr/lib/jvm/default/bin:/usr/bin/site_perl:/usr/bin/vendor_perl:/usr/bin/core_perl:/home/sourav/.rvm/bin:/home/sourav/.rvm/bin:/home/sourav/.gem/ruby/2.6.0/bin", "DBUS_SESSION_BUS_ADDRESS"=>"unix:path=/run/user/1000/bus", "MAIL"=>"/var/spool/mail/sourav", "_"=>"/home/sourav/.irb", "LINES"=>"24", "COLUMNS"=>"80"}

Which looks similar to:

> ENV.to_h
=> {"SHELL"=>"/bin/bash", "SESSION_MANAGER"=>"local/archlinux:@/tmp/.ICE-unix/613,unix/archlinux:/tmp/.ICE-unix/613", "COLORTERM"=>"truecolor", "XDG_CONFIG_DIRS"=>"/etc/xdg", "XDG_MENU_PREFIX"=>"xfce-", "SSH_AUTH_SOCK"=>"/tmp/ssh-Al0pdO1R5970/agent.622", "DESKTOP_SESSION"=>"Xfce Session", "SSH_AGENT_PID"=>"623", "GTK_MODULES"=>"canberra-gtk-module:canberra-gtk-module", "XDG_SEAT"=>"seat0", "PWD"=>"/home/sourav", "LOGNAME"=>"sourav", "XDG_SESSION_TYPE"=>"x11", "XAUTHORITY"=>"/home/sourav/.Xauthority", "HOME"=>"/home/sourav", "LANG"=>"en_GB.UTF-8", "XDG_CURRENT_DESKTOP"=>"XFCE", "VTE_VERSION"=>"5603", "INVOCATION_ID"=>"6d4dc7a91cc141e691436cb850e18f8d", "GLADE_CATALOG_PATH"=>":", "XDG_SESSION_CLASS"=>"user", "TERM"=>"xterm-256color", "USER"=>"sourav", "DISPLAY"=>":0.0", "SHLVL"=>"2", "XDG_VTNR"=>"1", "XDG_SESSION_ID"=>"1", "TILIX_ID"=>"f2480263-263e-408f-be36-8105e71256a6", "MOZ_PLUGIN_PATH"=>"/usr/lib/mozilla/plugins", "GLADE_MODULE_PATH"=>":", "XDG_RUNTIME_DIR"=>"/run/user/1000", "GLADE_PIXMAP_PATH"=>":", "JOURNAL_STREAM"=>"9:25041", "XDG_DATA_DIRS"=>"/usr/local/share:/usr/share", "PATH"=>"/usr/local/sbin:/usr/local/bin:/usr/bin:/usr/lib/jvm/default/bin:/usr/bin/site_perl:/usr/bin/vendor_perl:/usr/bin/core_perl:/home/sourav/.rvm/bin:/home/sourav/.rvm/bin:/home/sourav/.gem/ruby/2.6.0/bin", "DBUS_SESSION_BUS_ADDRESS"=>"unix:path=/run/user/1000/bus", "MAIL"=>"/var/spool/mail/sourav", "_"=>"/home/sourav/.irb", "LINES"=>"24", "COLUMNS"=>"80"}

But:

> ENV.to_h.eql?(ENV)
=> false

So what kind of object does ENV return?

like image 776
S.Goswami Avatar asked Mar 13 '26 02:03

S.Goswami


2 Answers

It's a custom Object with Hash-like functionality mostly implemented in C.

https://github.com/ruby/ruby/blob/trunk/hash.c#L4944

like image 135
fylooi Avatar answered Mar 15 '26 16:03

fylooi


There is nothing in the Ruby Language Specification that requires ENV to be implemented in any particular way. As long as it responds to the right messages in the right way, it can be anything it wants.

For example, in Rubinius, ENV is implemented in a class called Rubinius::EnvironmentVariables that implements part of the Hash protocol and also mixes in Enumerable: https://github.com/rubinius/rubinius/blob/master/core/env.rb .

like image 34
Jörg W Mittag Avatar answered Mar 15 '26 16:03

Jörg W Mittag



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!