An Introduction to Procs, by Peter Cooper

I have just watched the 27-min introduction on Procs by Peter Cooper, which is really a short section of his RubyReloaded course.


Here are my notes:

  • blocs are essentially nameless functions
  • the block_given? method can be used to find out if a block was passed to the current scope
  • blocks are anonymous functions which can be passed around and called at will
  • one can only pass a single block to a method, but one can pass multiple procs around (as arguments of a method)
  • will create a Proc object if there was a block passed in the current context (otherwise it will raise an error)
  • In Ruby 1.9, there are 4 ways to call (or run) proc objects:
  • a lambda is a proc object which respects argument arity, much like an anonymous method
  • in 1.8, “proc” (surprisingly) created a lambda (and not a Proc object)
  • in 1.9, “proc” is the same as doing (as we would expect)
  • a return in a proc will try to return from the context where the proc was defined (and not from where it is executed)
  • in a closure, ruby keeps references to variables. You can thus modify a variable’s value inside a proc even after the proc was defined. Thus this code will output “Marc”:



Got a comment?

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )


Connecting to %s

%d bloggers like this: