Class: Proc
Overview
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
Class Method Summary collapse
-
.new ⇒ Object
Creates a new
Procobject, bound to the current context.
Instance Method Summary collapse
-
#==(other_proc) ⇒ Boolean
Return
trueif prc is the same object as other_proc, or if they are both procs with the same body. -
#[] ⇒ Object
Invokes the block, setting the block’s parameters to the values in params using something close to method calling semantics.
-
#arity ⇒ Fixnum
Returns the number of arguments that would not be ignored.
-
#binding ⇒ Binding
Returns the binding associated with prc.
-
#call ⇒ Object
Invokes the block, setting the block’s parameters to the values in params using something close to method calling semantics.
-
#clone ⇒ Object
MISSING: documentation.
-
#dup ⇒ Object
MISSING: documentation.
-
#to_proc ⇒ Proc
Part of the protocol for converting objects to
Procobjects. -
#to_s ⇒ String
Shows the unique identifier for this proc, along with an indication of where the proc was defined.
Class Method Details
.new {|...| ... } ⇒ Proc .new ⇒ Proc
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"
8472 8473 8474 |
# File 'eval.c', line 8472 static VALUE proc_s_new(argc, argv, klass) int argc; |
Instance Method Details
#==(other_proc) ⇒ Boolean
Return true if prc is the same object as other_proc, or if they are both procs with the same body.
8720 8721 8722 |
# File 'eval.c', line 8720 static VALUE proc_eq(self, other) VALUE self, other; |
#call(params, ...) ⇒ Object #[](params, ...) ⇒ Object
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).
For procs created using Kernel.proc, generates an error if the wrong number of parameters are passed to a proc with multiple parameters. For procs created using Proc.new, 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 = Proc.new {|a,b| a}
a_proc.call(1,2,3)
produces:
prog.rb:5: wrong number of arguments (3 for 2) (ArgumentError)
from prog.rb:4:in `call'
from prog.rb:5
8649 8650 8651 |
# File 'eval.c', line 8649 static VALUE proc_call(proc, args) VALUE proc, args; |
#arity ⇒ Fixnum
Returns the number of arguments that would not be ignored. 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, return -n-1, where n is the number of mandatory arguments. A proc with no argument declarations is the same a block declaring || as its arguments.
Proc.new {}.arity #=> 0
Proc.new {||}.arity #=> 0
Proc.new {|a|}.arity #=> 1
Proc.new {|a,b|}.arity #=> 2
Proc.new {|a,b,c|}.arity #=> 3
Proc.new {|*a|}.arity #=> -1
Proc.new {|a,*b|}.arity #=> -2
8679 8680 8681 |
# File 'eval.c', line 8679 static VALUE proc_arity(proc) VALUE proc; |
#binding ⇒ Binding
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
eval("param", b) #=> 99
8812 8813 8814 |
# File 'eval.c', line 8812 static VALUE proc_binding(proc) VALUE proc; |
#call(params, ...) ⇒ Object #[](params, ...) ⇒ Object
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).
For procs created using Kernel.proc, generates an error if the wrong number of parameters are passed to a proc with multiple parameters. For procs created using Proc.new, 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 = Proc.new {|a,b| a}
a_proc.call(1,2,3)
produces:
prog.rb:5: wrong number of arguments (3 for 2) (ArgumentError)
from prog.rb:4:in `call'
from prog.rb:5
8649 8650 8651 |
# File 'eval.c', line 8649 static VALUE proc_call(proc, args) VALUE proc, args; |
#clone ⇒ Object
MISSING: documentation
8284 8285 8286 |
# File 'eval.c', line 8284 static VALUE proc_clone(self) VALUE self; |
#dup ⇒ Object
MISSING: documentation
8303 8304 8305 |
# File 'eval.c', line 8303 static VALUE proc_dup(self) VALUE self; |
#to_proc ⇒ Proc
Part of the protocol for converting objects to Proc objects. Instances of class Proc simply return themselves.
8788 8789 8790 |
# File 'eval.c', line 8788 static VALUE proc_to_self(self) VALUE self; |
#to_s ⇒ String
Shows the unique identifier for this proc, along with an indication of where the proc was defined.
8749 8750 8751 |
# File 'eval.c', line 8749 static VALUE proc_to_s(self) VALUE self; |