Method: Symbol#to_proc
- Defined in:
- proc.c
#to_proc ⇒ Object
Returns a Proc object which calls the method with name of self on the first parameter and passes the remaining parameters to the method.
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"]
1515 1516 1517 1518 1519 1520 1521 1522 1523 1524 1525 1526 1527 1528 1529 1530 1531 1532 1533 1534 1535 1536 1537 1538 1539 1540 1541 1542 |
# File 'proc.c', line 1515 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; } } |