Maintenance Ruby 3.3 gets fixes, but no new features. 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 _)
{
VALUE result = Qnil;
VALUE key = Qnil;
ENV_LOCK();
{
char **env = GET_ENVIRON(environ);
if (*env) {
const char *p = *env;
char *s = strchr(p, '=');
if (s) {
key = env_str_new(p, s-p);
VALUE val = env_str_new2(getenv(RSTRING_PTR(key)));
result = rb_assoc_new(key, val);
}
}
FREE_ENVIRON(environ);
}
ENV_UNLOCK();
if (!NIL_P(key)) {
env_delete(key);
}
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.