Preview Ruby 4.1 is a preview. It can change before the final release. Switch to Ruby 4.0 →
instance method
Thread#thread_variable_set
(untyped key, untyped value) -> untyped
thr.thread_variable_set(key, value)
thread.c
View on GitHub ↗
static VALUE
rb_thread_variable_set(VALUE thread, VALUE key, VALUE val)
{
VALUE locals;
VALUE symbol;
if (OBJ_FROZEN(thread)) {
rb_frozen_error_raise(thread, "can't modify frozen thread locals");
}
symbol = rb_to_symbol(key);
if (NIL_P(val)) {
if (LIKELY(!THREAD_LOCAL_STORAGE_INITIALISED_P(thread))) {
return Qnil;
}
locals = rb_thread_local_storage(thread);
rb_hash_delete(locals, symbol);
return Qnil;
}
locals = rb_thread_local_storage(thread);
return rb_hash_aset(locals, symbol, val);
}
Sets a thread local with key to value. Note that these are local to threads, and not to fibers. Please see Thread#thread_variable_get and Thread#[] for more information. Assigning nil deletes the variable.
Ruby version changes
-
4.1
-
Assigning
nilwithThread#thread_variable_setnow deletes the thread-local variable. [[Feature #19333]]
-
See also
#thread_variables
#to_s
#thread_variable_get
#value
also in
4.0 · 3.4 · 3.3 · 3.2 · 3.1 · 3.0