End of Life Ruby 3.2 has reached end of life and gets no more fixes. Switch to Ruby 4.0 →
class method
ENV.rassoc
ENV.rassoc(value) → [name, value] or nil
hash.c
View on GitHub ↗
static VALUE
env_rassoc(VALUE dmy, VALUE obj)
{
obj = rb_check_string_type(obj);
if (NIL_P(obj)) return Qnil;
VALUE result = Qnil;
ENV_LOCK();
{
char **env = GET_ENVIRON(environ);
while (*env) {
const char *p = *env;
char *s = strchr(p, '=');
if (s++) {
long len = strlen(s);
if (RSTRING_LEN(obj) == len && strncmp(s, RSTRING_PTR(obj), len) == 0) {
result = rb_assoc_new(rb_str_new(p, s-p-1), obj);
break;
}
}
env++;
}
FREE_ENVIRON(environ);
}
ENV_UNLOCK();
return result;
}
Returns a 2-element Array containing the name and value of the first found environment variable that has value value, if one exists:
Example
ENV.replace('foo' => '0', 'bar' => '0') ENV.rassoc('0') # => ["bar", "0"]
The order in which environment variables are examined is OS-dependent. See About Ordering.
Returns nil if there is no such environment variable.