Scopes, Universes, & Lunch Boxes; Procs vs Lambdas in Ruby

  1. Definitions of Code Block, Proc, Lambda, and Closure
  2. Constructions
  3. Calling a Code Block/Proc/Lambda
  4. Passing and Returning Procs
  • a code block is a block of code
  • a proc is an object that contains a code block
  • a lambda is a special type of proc
  • A closure is a function that: 1. can be passed around as a variable and 2. binds to the same scope in which it was created (more on that in this post). To different extends, code blocks, procs, and lambdas, can be seen as closures.

5. Scopes, Universes, and Lunch Boxes

6. Differences between a proc and a lambda

Tony Tony Chopper is here b/c he is cute!
  1. The ways they are constructed are different:
  • a regular proc is constructed by either Proc.new or proc
  • a lambda is constructed by either lambda or -> (stabby lambda)
  • a lambda return from the code block it contains
  • a proc return from the scope that calls it
An excellent example from The Well-Grounded Rubyist

--

--

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store