Blocks, Procs, and Lambdas
Lesson 1 of 1
  1. 1

    We learned a bit about blocks in Loops & Iterators and [Methods, Blocks, & Sorting](

  2. 2

    Good! There are a bunch of really useful Ruby methods that take blocks. One we haven’t covered yet is […] . The […] method takes a block and applies the expression in the block to every ele…

  3. 3

    Why do some methods accept a block and others don’t? It’s because methods that accept blocks have a way of transferring control from the calling method to the block and back again. We can build thi…

  4. 4

    You can also pass parameters to […] ! Check out the example in the editor. 1. The […] method is defined with one parameter, […] . 2. On line 9, we call the […] method and supply the…

  5. 5

    See? Even though this block business has a little more going on under the hood than we thought, it’s still pretty straightforward. Now that we know how methods accept blocks using the […] keyw…

  6. 6

    Remember when we told you that everything is an object in Ruby? Well, we sort of fibbed. Blocks are not objects, and this is one of the very few exceptions to the “everything is an object” rule i…

  7. 7

    Procs are easy to define! You just call […] and pass in the block you want to save. Here’s how we’d create a proc called […] that cubes a number (raises it to the third power): […] We c…

  8. 8

    Why bother saving our blocks as procs? There are two main advantages: 1. Procs are full-fledged objects, so they have all the powers and abilities of objects. (Blocks do not.) 2. Unlike blocks, pr…

  9. 9

    Okay! Time to take off the training wheels. […] You’re going to create your very own method that calls your very own proc! We’ll do this in two steps. Use the example above as a syntax reminder.

  10. 10

    Nice work! Calling a proc with a method isn’t too tricky. However, there’s an even easier way. Unlike blocks, we can call procs directly by using Ruby’s […] method. Check it out! […] Remem…

  11. 11

    Now that you’re learning some of the more complex parts of the Ruby language, you can combine them to work some truly arcane magicks. For instance, remember when we told you that you could pass a …

  12. 12

    Like procs, lambdas are objects. The similarities don’t stop there: with the exception of a bit of syntax and a few behavioral quirks, lambdas are identical to procs. Check out the code in the edi…

  13. 13

    Lambdas are defined using the following syntax: […] Lambdas are useful in the same situations in which you’d use a proc. We’ll cover the differences between lambdas and procs in the next exerci…

  14. 14

    If you’re thinking that procs and lambdas look super similar, that’s because they are! There are only two main differences. First, a lambda checks the number of arguments passed to it, while a pro…

  15. 15

    Great work! You’ve written your own lambda and seen how to pass it to a method. Now it’s time for you to write a lambda and pass it to a method! If you think this will be a lot like what you’ve …

  16. 16

    All this talk of blocks, procs, and lambdas might have your head spinning. Let’s take a minute to clarify exactly what each one is: 1. A block is just a bit of code between […] .. […] or [….

  17. 17

    You’re basically a Ruby block master at this point. This should be pretty easy!

  18. 18

    Now let’s tackle procs. First step: create one!

  19. 19

    Perfect! Now let’s pass our proc to a method.

  20. 20

    Second verse: same as the first! Let’s go ahead and make ourselves a […] .

  21. 21

    Home stretch! Now let’s pass our lambda to a method, as well.