Ruby Lambda Tutorial
Similar to Procs, Lambdas allow you to store functions inside a variable and call the method from other methods. In this lesson, we are going to go over Lambdas and how to integrate them into a Ruby program.
Guide Tasks
  • Read Tutorial
  • Watch Guide Video
Video locked
This video is viewable to users with a Bottega Bootcamp license

Similar to Procs, Lambdas allow you to store functions inside a variable and call the method from other parts of a program. In this lesson we are going to discuss Lambdas and how to integrate them into a Ruby program.

To get started, I'm going to use the same example as last lesson, but with a different syntax.

full_name = lambda { |first, last| first + " " + last }

You can also call Lambdas in the same way as Procs:

p full_name["jordan", "hudgens"]

If you notice the implementation is nearly identical to using Procs, with the only difference being the use of the word lambda instead of Proc.new.

Stabby Lambdas

It's a common practice in real world projects to use a different syntax though: the stabby lambda. Here is how you can use stabby lambdas.

full_name = -> (first, last) { first + " " + last }

If you run this code, it runs exactly the same way as the previous one.

Like Procs, you can also run the lambdas with the call syntax:

p first_name.call("jordan", "hudgens")

So, that's how you create lambdas in Ruby with both the regular and stabby syntax.