Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Getting Closure: Ruby's Blocks, Procs and Lambdas

Getting Closure: Ruby's Blocks, Procs and Lambdas

This was a talk given at BostonRB. It was an introduction to an aspect of Ruby that is not talked about very often but that represents one of the distinctive features of the language.

Johnny Boursiquot

May 14, 2013
Tweet

More Decks by Johnny Boursiquot

Other Decks in Technology

Transcript

  1. Johnny Boursiquot (boar-cee-co) Technologist Tech Director/Software Architect at a local

    interactive agency (MAARK) @jboursiquot / jboursiquot.com Tuesday, May 28, 13
  2. What we’ll talk about Closures - What they are, their

    value Blocks Procs Lambdas Where to go (literally) from here Tuesday, May 28, 13
  3. A closure is... A function or method that can be

    passed around like an object It remembers variables that were in scope when it was created Tuesday, May 28, 13
  4. December 22, 2003 A Conversation with Yukihiro Matsumoto, Part III

    http://www.artima.com/intv/closures.html Tuesday, May 28, 13
  5. “Blocks are basically nameless functions...Basically, you can pass a nameless

    function to another function, and then that function can invoke the passed-in nameless function.” Tuesday, May 28, 13
  6. “Many other languages do this style of programming. In Ruby,

    the difference is mainly a different kind of syntax...” Tuesday, May 28, 13
  7. Block Syntax Block helps you decouple logic from iteration They

    take parameters -- like methods They appear after method invocation -- as if an additional parameter to your method call Tuesday, May 28, 13
  8. Block Syntax Block called once for each iteration of array

    Each element passed into block as a variable Variables defined inside block are local to that block Tuesday, May 28, 13
  9. Block Syntax “yield” is used in methods that accept blocks

    as parameters to transfer control to the block and back Tuesday, May 28, 13
  10. Block Rules Parameters to a block are always local to

    that block, even if variable names are shared with the outside. Tuesday, May 28, 13
  11. Block Rules You can define block-local variables by putting them

    after a semicolon in the block's parameter list. Tuesday, May 28, 13
  12. Hmmm.... How can we not repeat ourselves whenever we need

    to double a number? Tuesday, May 28, 13
  13. A closure is... A function or method that can be

    passed around like an object It remembers variables that were in scope when it was created Tuesday, May 28, 13
  14. Proc Syntax We turn the logic we need into a

    Proc so we can pass it around We use “&” when calling our collect method to convert the Proc back into a block because it expects one Tuesday, May 28, 13
  15. Proc’s Benefits Unlike blocks, they are objects that can be

    passed around Unlike blocks, they can be called upon multiple times, making our code DRYer Tuesday, May 28, 13
  16. Procs vs Lambdas Lambdas check the number of arguments passed

    in. Procs do not. Procs ignore any missing arguments, assigning nil to them. Lamdas return to the calling method, Procs return immediately without going back to the caller. Tuesday, May 28, 13
  17. Behavior of Return Statements Calling return from inside a proc

    will return from calling method Calling return from lambda will return only from the lambda Tuesday, May 28, 13
  18. Closure Scope Retention Create a copy of all variables at

    time of definition Retain reference to variables (Ruby’s uses this method) Tuesday, May 28, 13