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
| Public Methods | |
|---|---|
| == | Return true if prc is the same object as other_proc, or if they are both procs with the same body. |
| [] | 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). |
| arity | 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. |
| binding | Returns the binding associated with prc. Note that Kernel#eval accepts either a Proc or a Binding object as its second parameter. |
| call | 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). |
| clone | MISSING: documentation |
| dup | MISSING: documentation |
| new | 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. |
| to_ |
Part of the protocol for converting objects to Proc objects. Instances of class Proc simply return themselves. |
| to_ |
Shows the unique identifier for this proc, along with an indication of where the proc was defined. |
<code/>and<pre/>for code samples.