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
-
.new {|...| ... } ⇒ Proc
constructor
Creates a new
Proc
object, bound to the current context.
Instance Attribute Summary
-
#lambda? ⇒ Boolean
readonly
Returns
true
for aProc
object for which argument handling is rigid.
Instance Method Summary
-
#call(params,...) ⇒ Object
Alias for #[].
-
#[](params,...) ⇒ Object
(also: #call, #===, #yield)
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 mandatory arguments.
-
#binding ⇒ Binding
Returns the binding associated with prc.
-
#call(params,...) ⇒ Object
Alias for #[].
-
#curry ⇒ Proc
Returns a curried proc.
-
#hash ⇒ Integer
Returns a hash value corresponding to proc body.
-
#inspect ⇒ String
Alias for #to_s.
-
#parameters ⇒ Array
Returns the parameter information of this proc.
-
#source_location ⇒ Array, Fixnum
Returns the Ruby source filename and line number containing this proc or
nil
if this proc was not defined in Ruby (i.e. -
#to_proc ⇒ Proc
Part of the protocol for converting objects to
Proc
objects. -
#to_s ⇒ String
(also: #inspect)
Returns the unique identifier for this proc, along with an indication of where the proc was defined.
-
#yield(params,...) ⇒ Object
Alias for #[].
Constructor Details
.new {|...| ... } ⇒ Proc
.new ⇒ Proc
Proc
.new ⇒ Proc
Creates a new Proc
object, bound to the current context. 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"
Instance Attribute Details
#lambda? ⇒ Boolean
(readonly)
Returns true
for a Proc
object for which argument handling is rigid. Such procs are typically generated by lambda
.
A Proc object generated by proc
ignores extra arguments.
proc {|a,b| [a,b] }.call(1,2,3) #=> [1,2]
It provides nil
for missing arguments.
proc {|a,b| [a,b] }.call(1) #=> [1,nil]
It expands a single array argument.
proc {|a,b| [a,b] }.call([1,2]) #=> [1,2]
A Proc object generated by lambda
doesn't have such tricks.
lambda {|a,b| [a,b] }.call(1,2,3) #=> ArgumentError
lambda {|a,b| [a,b] }.call(1) #=> ArgumentError
lambda {|a,b| [a,b] }.call([1,2]) #=> ArgumentError
lambda?
is a predicate for the tricks. It returns true
if no tricks apply.
lambda {}.lambda? #=> true
proc {}.lambda? #=> false
.new is the same as proc
.
Proc.new {}.lambda? #=> false
lambda
, proc
and .new preserve the tricks of a Proc
object given by &
argument.
lambda(&lambda {}).lambda? #=> true
proc(&lambda {}).lambda? #=> true
Proc.new(&lambda {}).lambda? #=> true
lambda(&proc {}).lambda? #=> false
proc(&proc {}).lambda? #=> false
Proc.new(&proc {}).lambda? #=> false
A Proc object generated by &
argument has the tricks
def n(&b) b.lambda? end
n {} #=> false
The &
argument preserves the tricks if a Proc
object is given by &
argument.
n(&lambda {}) #=> true
n(&proc {}) #=> false
n(&Proc.new {}) #=> false
A Proc object converted from a method has no tricks.
def m() end
method(:m).to_proc.lambda? #=> true
n(&method(:m)) #=> true
n(&method(:m).to_proc) #=> true
define_method
is treated the same as method definition. The defined method has no tricks.
class C
define_method(:d) {}
end
C.new.d(1,2) #=> ArgumentError
C.new.method(:d).to_proc.lambda? #=> true
define_method
always defines a method without the tricks, even if a non-lambda Proc
object is given. This is the only exception for which the tricks are not preserved.
class C
define_method(:e, &proc {})
end
C.new.e(1,2) #=> ArgumentError
C.new.method(:e).to_proc.lambda? #=> true
This exception insures that methods never have tricks and makes it easy to have wrappers to define methods that behave as usual.
class C
def self.def2(name, &body)
define_method(name, &body)
end
def2(:f) {}
end
C.new.f(1,2) #=> ArgumentError
The wrapper def2 defines a method which has no tricks.
Instance Method Details
Also known as: #call, #===, #yield
Invokes the block, setting the block's parameters to the values in params using something close to method calling semantics. Returns the value of the last expression evaluated in the block.
a_proc = Proc.new {|scalar, *values| values.map {|value| value*scalar } }
a_proc.call(9, 1, 2, 3) #=> [9, 18, 27]
a_proc[9, 1, 2, 3] #=> [9, 18, 27]
a_proc.(9, 1, 2, 3) #=> [9, 18, 27]
a_proc.yield(9, 1, 2, 3) #=> [9, 18, 27]
Note that prc.()
invokes prc.call()
with the parameters given. It's syntactic sugar to hide “call”.
For procs created using lambda
or ->()
an error is generated if the wrong number of parameters are passed to the proc. For procs created using .new or Kernel.proc, extra parameters are silently discarded and missing parameters are set to nil
.
a_proc = proc {|a,b| [a,b] }
a_proc.call(1) #=> [1, nil]
a_proc = lambda {|a,b| [a,b] }
a_proc.call(1) # ArgumentError: wrong number of arguments (given 1, expected 2)
See also #lambda?.
#arity ⇒ Fixnum
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
#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
Alias for #[].
#curry ⇒ Proc
#curry(arity) ⇒ Proc
Proc
#curry(arity) ⇒ Proc
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 (given 4, expected 3)
p b.curry(5) #=> wrong number of arguments (given 5, expected 3)
p b.curry(1) #=> wrong number of arguments (given 1, expected 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 (given 1, expected 3)
b = proc { :foo }
p b.curry[] #=> :foo
#hash ⇒ Integer
Returns a hash value corresponding to proc body.
See also Object#hash.
Alias for #to_s.
#parameters ⇒ Array
Returns the parameter information of this proc.
prc = lambda{|x, y=42, *other|}
prc.parameters #=> [[:req, :x], [:opt, :y], [:rest, :other]]
#source_location ⇒ Array, Fixnum
Returns the Ruby source filename and line number containing this proc or nil
if this proc was not defined in Ruby (i.e. native).
#to_proc ⇒ Proc
Part of the protocol for converting objects to Proc
objects. Instances of class Proc
simply return themselves.
#to_s ⇒ String Also known as: #inspect
Returns the unique identifier for this proc, along with an indication of where the proc was defined.
Alias for #[].