Preview Ruby 4.1 is a preview. It can change before the final release. Switch to Ruby 4.0 →
instance method
Thread#thread_variable?
(::interned key) -> bool
thr.thread_variable?(key) → true or false
thread.c
View on GitHub ↗
static VALUE
rb_thread_variable_p(VALUE thread, VALUE key)
{
VALUE locals;
VALUE symbol = rb_to_symbol(key);
if (LIKELY(!THREAD_LOCAL_STORAGE_INITIALISED_P(thread))) {
return Qfalse;
}
locals = rb_thread_local_storage(thread);
return RBOOL(rb_hash_lookup(locals, symbol) != Qnil);
}
Returns true if the given string (or symbol) exists as a thread-local variable.
Example
me = Thread.current me.thread_variable_set(:oliver, "a") me.thread_variable?(:oliver) #=> true me.thread_variable?(:stanley) #=> false
Note that these are not fiber local variables. Please see Thread#[] and Thread#thread_variable_get for more details.
See also
#terminate
#thread_variable_get
#stop?
#thread_variables
also in
4.0 · 3.4 · 3.3 · 3.2 · 3.1 · 3.0