End of Life Ruby 3.0 has reached end of life and gets no more fixes. Switch to Ruby 4.0 →
class method
ENV.key
ENV.key(value) → name or nil
hash.c
View on GitHub ↗
static VALUE
env_key(VALUE dmy, VALUE value)
{
char **env;
VALUE str;
SafeStringValue(value);
env = GET_ENVIRON(environ);
while (*env) {
char *s = strchr(*env, '=');
if (s++) {
long len = strlen(s);
if (RSTRING_LEN(value) == len && strncmp(s, RSTRING_PTR(value), len) == 0) {
str = env_str_new(*env, s-*env-1);
FREE_ENVIRON(environ);
return str;
}
}
env++;
}
FREE_ENVIRON(environ);
return Qnil;
}
Returns the name of the first environment variable with value, if it exists:
Example
ENV.replace('foo' => '0', 'bar' => '0') ENV.key('0') # => "foo"
The order in which environment variables are examined is OS-dependent. See About Ordering.
Returns nil if there is no such value.
Raises an exception if value is invalid:
Example
ENV.key(Object.new) # raises TypeError (no implicit conversion of Object into String)