Pass Multiple Blocks to a Method by Leveraging Lambdas in Ruby

In many cases, lambdas are the most effective when they’re used in conjunction with methods. A method is limited to taking a single block in Ruby, however by leveraging lambdas you are able to pass in as many blocks as needed.

Summary

Implement a method that can accept multiple blocks by leveraging lambdas.

Exercise File

Code File

Exercise Description

Build a method that takes multiple lambdas as arguments in order to illustrate how a single method can be passed multiple blocks as arguments.

Real World Usage

By default, a method in Ruby is limited to only receiving a single block as an argument. This is where lambdas (also known as closures in other languages) are able to extend the functionality of a single method. A method can take an unlimited number of lambdas as argument, so therefore lambdas allow you to give plain Ruby methods the ability to accept multiple blocks as arguments. This exercise helps to showcase this behavior by building a User tracking solution.

Solution

Can be found on the solutions branch on github.

Jordan Hudgens

view all post
Leave a comment

Please be polite. We appreciate that.

By Daniele Zedda • 18 February

← PREV POST

By Daniele Zedda • 18 February

NEXT POST → 34
Share on