Follow Techotopia on Twitter

On-line Guides
All Guides
eBook Store
iOS / Android
Linux for Beginners
Office Productivity
Linux Installation
Linux Security
Linux Utilities
Linux Virtualization
Linux Kernel
System/Network Admin
Programming
Scripting Languages
Development Tools
Web Development
GUI Toolkits/Desktop
Databases
Mail Systems
openSolaris
Eclipse Documentation
Techotopia.com
Virtuatopia.com
Answertopia.com

How To Guides
Virtualization
General System Admin
Linux Security
Linux Filesystems
Web Servers
Graphics & Desktop
PC Hardware
Windows
Problem Solutions
Privacy Policy

  




 

 

Ruby Programming
Previous Page Home Next Page
class Proc
Parent: Object
Version: 1.6

Index:

new [ ] arity call


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 genTimes(factor)
  return Proc.new {|n| n*factor }
end
times3 = genTimes(3)
times5 = genTimes(5)
times3.call(12) 36
times5.call(5) 25
times3.call(times5.call(4)) 60

class methods
new Proc.new [{| | block } ] -> aProc

Creates a new Proc object, bound to the current context. It 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 procFrom
  Proc.new
end
aProc = procFrom { "hello" }
aProc.call "hello"

instance methods
[ ] prc[ [ params ]* ] -> anObject

Synonym for Proc.call.

arity prc.arity -> anInteger

Returns the number of arguments required by the block. If the block takes no arguments, returns -1. If it takes one argument, returns -2. Otherwise, returns a positive argument count unless the last argument is prefixed with *, in which case the argument count is negated. The number of required arguments is anInteger for positive values, and ( anInteger +1).abs otherwise.

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

call prc.call( [ params ]* ) -> anObject

Invokes the block, setting the block's parameters to the values in params using the same rules as used by parallel assignment. Returns the value of the last expression evaluated in the block.

aProc = Proc.new {|a, *b| b.collect {|i| i*a }}
aProc.call(9, 1, 2, 3) [9, 18, 27]
aProc[9, 1, 2, 3] [9, 18, 27]


Ruby Programming
Previous Page Home Next Page

 
 
  Published under the terms of the Open Publication License Design by Interspire