FAQ: Blocks, Procs, and Lambdas - The Ruby Lambda

This community-built FAQ covers the “The Ruby Lambda” exercise from the lesson “Blocks, Procs, and Lambdas”.

Paths and Courses
This exercise can be found in the following Codecademy content:

Learn Ruby

FAQs on the exercise The Ruby Lambda

There are currently no frequently asked questions associated with this exercise – that’s where you come in! You can contribute to this section by offering your own questions, answers, or clarifications on this exercise. Ask or answer a question by clicking reply (reply) below.

If you’ve had an “aha” moment about the concepts, formatting, syntax, or anything else with this exercise, consider sharing those insights! Teaching others and answering their questions is one of the best ways to learn and stay sharp.

Join the Discussion. Help a fellow learner on their journey.

Ask or answer a question about this exercise by clicking reply (reply) below!

Agree with a comment or answer? Like (like) to up-vote the contribution!

Need broader help or resources? Head here.

Looking for motivation to keep learning? Join our wider discussions.

Learn more about how to use this guide.

Found a bug? Report it!

Have a question about your account or billing? Reach out to our customer support team!

None of the above? Find out where to ask other questions here!

Hi people !

I understood the exercice but there is something I can’t figure it out…

How and what is the thing which links the third line with the sixth one ?

image

Maybe it’s a dumb question but I really need your help haha… Thanks !

1 Like

a_lambda is the parameter. The object it receives has its own block of code. It is that code block that executes in the method.

5 Likes

Thank you for this response mtf !

1 Like

Buenos días, puede entenderse que en ejecución así funciona este ejercicio ?
image

Hola! Lo siento para mi mal español, pero sí. a_lambda es el parámetro y lamda es el argumento. El parámetro es un marcador de posición para el argumento.

So you nest a block in a block by putting it as the argument? I think that what is exercise is telling us something without actually saying it directly. And it is that you can have a block, more specifically a Proc or a Lambda, as an argument in a method. Why would one need to do that, I do not know yet.

This may seem confusing since we literally just learned what a ‘lambda’ is and now we are ‘interpolating’ blocks in arguments in methods, in which a method can be thought of as a block, but that is what I think is going on here. The lambda is the argument, to the method.

I’m glad you’ve figured it out, @chadw1ck! All I have to contribute is my annoyance with the overuse of the word “lambda” throughout the example - it makes it very hard to follow what is happening. For anyone else, I believe that lambda_demo is the lambda, and a_lambda is a parameter. If I substitute other words to make sense of it, it looks like this:

def demo(my_parameter) puts "I'm the first!" my_parameter.call end demo(lambda { puts "I'm the second!" })
1 Like

Hi everyone!

I don’t understand what’s happening in this exercise or the explanations given above. I’d be very grateful if someone could put it in plain terms for me please.

I understand that we’ve defined a method called lambda_demo, which takes one parameter - (a_lambda), but why do we suddenly call (a_lambda) on line 3?

On line 6 we call lambda_demo with lambda, but how does Ruby know what lambda is? Don’t we need to define lambda somewhere?

Are we defining lambda and a calling it with the same line of code on line 6?

Thanks.

lambda_demo() is the method being called in line 6. The argument to that call is where the lambda (a block) is defined. It is then called within the demo method.

1 Like