End of Life Ruby 3.0 has reached end of life and gets no more fixes. Switch to Ruby 4.0 →
class method
ENV.shift
ENV.shift → [name, value] or nil
hash.c
View on GitHub ↗
static VALUE
env_shift(VALUE _)
{
char **env;
VALUE result = Qnil;
env = GET_ENVIRON(environ);
if (*env) {
char *s = strchr(*env, '=');
if (s) {
VALUE key = env_str_new(*env, s-*env);
VALUE val = env_str_new2(getenv(RSTRING_PTR(key)));
env_delete(key);
result = rb_assoc_new(key, val);
}
}
FREE_ENVIRON(environ);
return result;
}
Removes the first environment variable from ENV and returns a 2-element Array containing its name and value:
Example
ENV.replace('foo' => '0', 'bar' => '1') ENV.to_hash # => {'bar' => '1', 'foo' => '0'} ENV.shift # => ['bar', '1'] ENV.to_hash # => {'foo' => '0'}
Exactly which environment variable is “first” is OS-dependent. See About Ordering.
Returns nil if the environment is empty.