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
Instance Method Summary collapse
-
#== ⇒ Object
FIX: Incomplete.
-
#[] ⇒ Object
call-seq: prc.call(params, …) -> obj prc[params, …] -> obj.
-
#arity ⇒ Object
FIX: Incomplete.
-
#call ⇒ Object
call-seq: prc.call(params, …) -> obj prc[params, …] -> obj.
-
#initialize(func) ⇒ Proc
constructor
call-seq: Proc.new {|…| block } -> proc.
-
#to_proc ⇒ Object
call-seq: proc.to_proc -> proc.
Constructor Details
#initialize(func) ⇒ Proc
call-seq:
Proc.new {|...| block } -> proc
Creates a new Proc object, bound to the current context.
4699 4700 4701 |
# File 'lib/source/ruby.rb', line 4699 def initialize(func) `this._block=func` end |
Instance Method Details
#[] ⇒ Object
call-seq:
prc.call(params, ...) -> obj
prc[params, ...] -> obj
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.
proc = Proc.new {|x| x * 100 }
proc[4] #=> 400
4720 4721 4722 |
# File 'lib/source/ruby.rb', line 4720 def []() `this._block.apply(this,arguments)` end |
#call ⇒ Object
call-seq:
prc.call(params, ...) -> obj
prc[params, ...] -> obj
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. See also Proc#yield.
proc = Proc.new {|x| x * 100 }
proc.call(4) #=> 400
4742 4743 4744 |
# File 'lib/source/ruby.rb', line 4742 def call `this._block.apply(this,arguments)` end |
#to_proc ⇒ Object
call-seq:
proc.to_proc -> proc
Part of the protocol for converting objects to Proc objects. Instances of class Proc simply return themselves.
4752 4753 4754 |
# File 'lib/source/ruby.rb', line 4752 def to_proc return self end |