Object
Proc
objects are blocks of code that have been bound to a set
of local variables. Once bound, the code may be called in different
contexts and still access those variables.
def gen_times(factor) return Proc.new {|n| n*factor } end times3 = gen_times(3) times5 = gen_times(5) times3.call(12) #=> 36 times5.call(5) #=> 25 times3.call(times5.call(4)) #=> 60
Creates a new Proc
object, bound to the current context.
Proc::new
may be called without a block only within a method
with an attached block, in which case that block is converted to the
Proc
object.
def proc_from Proc.new end proc = proc_from { "hello" } proc.call #=> "hello"
static VALUE rb_proc_s_new(int argc, VALUE *argv, VALUE klass) { VALUE block = proc_new(klass, FALSE); rb_obj_call_init(block, argc, argv); return block; }
Invokes the block with obj
as the proc's parameter like #call. It is to allow a proc object to
be a target of when
clause in a case statement.
static VALUE proc_call(int argc, VALUE *argv, VALUE procval) { VALUE vret; rb_proc_t *proc; rb_block_t *blockptr = 0; rb_iseq_t *iseq; VALUE passed_procval; GetProcPtr(procval, proc); iseq = proc->block.iseq; if (BUILTIN_TYPE(iseq) == T_NODE || iseq->param.flags.has_block) { if (rb_block_given_p()) { rb_proc_t *passed_proc; RB_GC_GUARD(passed_procval) = rb_block_proc(); GetProcPtr(passed_procval, passed_proc); blockptr = &passed_proc->block; } } vret = rb_vm_invoke_proc(GET_THREAD(), proc, argc, argv, blockptr); RB_GC_GUARD(procval); return vret; }
Invokes the block, setting the block’s parameters to the values in params using something close to method calling semantics. Generates a warning if multiple values are passed to a proc that expects just one (previously this silently converted the parameters to an array). Note that prc.() invokes prc.call() with the parameters given. It’s a syntax sugar to hide “call”.
For procs created using lambda
or ->()
an
error is generated if the wrong number of parameters are passed to a Proc with multiple parameters. For procs created
using Proc.new
or Kernel.proc
, extra parameters
are silently discarded.
Returns the value of the last expression evaluated in the block. See also
Proc#yield
.
a_proc = Proc.new {|a, *b| b.collect {|i| i*a }} a_proc.call(9, 1, 2, 3) #=> [9, 18, 27] a_proc[9, 1, 2, 3] #=> [9, 18, 27] a_proc = lambda {|a,b| a} a_proc.call(1,2,3)
produces:
prog.rb:4:in `block in <main>': wrong number of arguments (3 for 2) (ArgumentError) from prog.rb:5:in `call' from prog.rb:5:in `<main>'
static VALUE proc_call(int argc, VALUE *argv, VALUE procval) { VALUE vret; rb_proc_t *proc; rb_block_t *blockptr = 0; rb_iseq_t *iseq; VALUE passed_procval; GetProcPtr(procval, proc); iseq = proc->block.iseq; if (BUILTIN_TYPE(iseq) == T_NODE || iseq->param.flags.has_block) { if (rb_block_given_p()) { rb_proc_t *passed_proc; RB_GC_GUARD(passed_procval) = rb_block_proc(); GetProcPtr(passed_procval, passed_proc); blockptr = &passed_proc->block; } } vret = rb_vm_invoke_proc(GET_THREAD(), proc, argc, argv, blockptr); RB_GC_GUARD(procval); return vret; }
Returns the number of mandatory arguments. If the block is declared to take
no arguments, returns 0. If the block is known to take exactly n arguments,
returns n. If the block has optional arguments, returns -n-1, where n is
the number of mandatory arguments, with the exception for blocks that are
not lambdas and have only a finite number of optional arguments; in this
latter case, returns n. Keywords arguments will considered as a single
additional argument, that argument being mandatory if any keyword argument
is mandatory. A proc
with no argument declarations is the same
as a block declaring ||
as its arguments.
proc {}.arity #=> 0 proc { || }.arity #=> 0 proc { |a| }.arity #=> 1 proc { |a, b| }.arity #=> 2 proc { |a, b, c| }.arity #=> 3 proc { |*a| }.arity #=> -1 proc { |a, *b| }.arity #=> -2 proc { |a, *b, c| }.arity #=> -3 proc { |x:, y:, z:0| }.arity #=> 1 proc { |*a, x:, y:0| }.arity #=> -2 proc { |x=0| }.arity #=> 0 lambda { |x=0| }.arity #=> -1 proc { |x=0, y| }.arity #=> 1 lambda { |x=0, y| }.arity #=> -2 proc { |x=0, y=0| }.arity #=> 0 lambda { |x=0, y=0| }.arity #=> -1 proc { |x, y=0| }.arity #=> 1 lambda { |x, y=0| }.arity #=> -2 proc { |(x, y), z=0| }.arity #=> 1 lambda { |(x, y), z=0| }.arity #=> -2 proc { |a, x:0, y:0| }.arity #=> 1 lambda { |a, x:0, y:0| }.arity #=> -2
static VALUE proc_arity(VALUE self) { int arity = rb_proc_arity(self); return INT2FIX(arity); }
Returns the binding associated with prc. Note that
Kernel#eval
accepts either a Proc
or a
Binding
object as its second parameter.
def fred(param) proc {} end b = fred(99) eval("param", b.binding) #=> 99
static VALUE proc_binding(VALUE self) { rb_proc_t *proc; VALUE bindval; rb_binding_t *bind; rb_iseq_t *iseq; GetProcPtr(self, proc); iseq = proc->block.iseq; if (RB_TYPE_P((VALUE)iseq, T_NODE)) { if (!IS_METHOD_PROC_NODE((NODE *)iseq)) { rb_raise(rb_eArgError, "Can't create Binding from C level Proc"); } iseq = rb_method_get_iseq(RNODE(iseq)->u2.value); } bindval = rb_binding_alloc(rb_cBinding); GetBindingPtr(bindval, bind); bind->env = proc->envval; bind->blockprocval = proc->blockprocval; if (RUBY_VM_NORMAL_ISEQ_P(iseq)) { bind->path = iseq->location.path; bind->first_lineno = FIX2INT(rb_iseq_first_lineno(iseq->self)); } else { bind->path = Qnil; bind->first_lineno = 0; } return bindval; }
Invokes the block, setting the block’s parameters to the values in params using something close to method calling semantics. Generates a warning if multiple values are passed to a proc that expects just one (previously this silently converted the parameters to an array). Note that prc.() invokes prc.call() with the parameters given. It’s a syntax sugar to hide “call”.
For procs created using lambda
or ->()
an
error is generated if the wrong number of parameters are passed to a Proc with multiple parameters. For procs created
using Proc.new
or Kernel.proc
, extra parameters
are silently discarded.
Returns the value of the last expression evaluated in the block. See also
Proc#yield
.
a_proc = Proc.new {|a, *b| b.collect {|i| i*a }} a_proc.call(9, 1, 2, 3) #=> [9, 18, 27] a_proc[9, 1, 2, 3] #=> [9, 18, 27] a_proc = lambda {|a,b| a} a_proc.call(1,2,3)
produces:
prog.rb:4:in `block in <main>': wrong number of arguments (3 for 2) (ArgumentError) from prog.rb:5:in `call' from prog.rb:5:in `<main>'
static VALUE proc_call(int argc, VALUE *argv, VALUE procval) { VALUE vret; rb_proc_t *proc; rb_block_t *blockptr = 0; rb_iseq_t *iseq; VALUE passed_procval; GetProcPtr(procval, proc); iseq = proc->block.iseq; if (BUILTIN_TYPE(iseq) == T_NODE || iseq->param.flags.has_block) { if (rb_block_given_p()) { rb_proc_t *passed_proc; RB_GC_GUARD(passed_procval) = rb_block_proc(); GetProcPtr(passed_procval, passed_proc); blockptr = &passed_proc->block; } } vret = rb_vm_invoke_proc(GET_THREAD(), proc, argc, argv, blockptr); RB_GC_GUARD(procval); return vret; }
Returns a curried proc. If the optional arity argument is given, it determines the number of arguments. A curried proc receives some arguments. If a sufficient number of arguments are supplied, it passes the supplied arguments to the original proc and returns the result. Otherwise, returns another curried proc that takes the rest of arguments.
b = proc {|x, y, z| (x||0) + (y||0) + (z||0) } p b.curry[1][2][3] #=> 6 p b.curry[1, 2][3, 4] #=> 6 p b.curry(5)[1][2][3][4][5] #=> 6 p b.curry(5)[1, 2][3, 4][5] #=> 6 p b.curry(1)[1] #=> 1 b = proc {|x, y, z, *w| (x||0) + (y||0) + (z||0) + w.inject(0, &:+) } p b.curry[1][2][3] #=> 6 p b.curry[1, 2][3, 4] #=> 10 p b.curry(5)[1][2][3][4][5] #=> 15 p b.curry(5)[1, 2][3, 4][5] #=> 15 p b.curry(1)[1] #=> 1 b = lambda {|x, y, z| (x||0) + (y||0) + (z||0) } p b.curry[1][2][3] #=> 6 p b.curry[1, 2][3, 4] #=> wrong number of arguments (4 for 3) p b.curry(5) #=> wrong number of arguments (5 for 3) p b.curry(1) #=> wrong number of arguments (1 for 3) b = lambda {|x, y, z, *w| (x||0) + (y||0) + (z||0) + w.inject(0, &:+) } p b.curry[1][2][3] #=> 6 p b.curry[1, 2][3, 4] #=> 10 p b.curry(5)[1][2][3][4][5] #=> 15 p b.curry(5)[1, 2][3, 4][5] #=> 15 p b.curry(1) #=> wrong number of arguments (1 for 3) b = proc { :foo } p