Maintenance Ruby 3.4 gets fixes, but no new features. Switch to Ruby 4.0 →
instance method
Symbol#to_proc
() -> ::Proc
to_proc
string.c
View on GitHub ↗
VALUE
rb_sym_to_proc(VALUE sym)
{
static VALUE sym_proc_cache = Qfalse;
enum {SYM_PROC_CACHE_SIZE = 67};
VALUE proc;
long index;
ID id;
if (!sym_proc_cache) {
sym_proc_cache = rb_ary_hidden_new(SYM_PROC_CACHE_SIZE * 2);
rb_vm_register_global_object(sym_proc_cache);
rb_ary_store(sym_proc_cache, SYM_PROC_CACHE_SIZE*2 - 1, Qnil);
}
id = SYM2ID(sym);
index = (id % SYM_PROC_CACHE_SIZE) << 1;
if (RARRAY_AREF(sym_proc_cache, index) == sym) {
return RARRAY_AREF(sym_proc_cache, index + 1);
}
else {
proc = sym_proc_new(rb_cProc, ID2SYM(id));
RARRAY_ASET(sym_proc_cache, index, sym);
RARRAY_ASET(sym_proc_cache, index + 1, proc);
return proc;
}
}
Returns a Proc object which calls the method with name of self on the first parameter and passes the remaining parameters to the method.
Example
proc = :to_s.to_proc # => #<Proc:0x000001afe0e48680(&:to_s) (lambda)> proc.call(1000) # => "1000" proc.call(1000, 16) # => "3e8" (1..3).collect(&:to_s) # => ["1", "2", "3"]
Ruby version changes
-
3.0
-
Symbol#to_procnow returns a lambdaProc. [Feature #16260]
-