FAQ: Blocks, Procs, and Lambdas - Lambdas vs. Procs

This community-built FAQ covers the “Lambdas vs. Procs” 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 Lambdas vs. Procs

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!

didn’t get the diffrence between lambdas and procs, can you elaborate using analogies or other examples

5 Likes

Hi there,

Same as Asimeir, I’m a bit confused about lambdas and Procs differences …

If someone can give us a more understandable explanation. I’ll be pleased

:slight_smile:

2 Likes

Someone can correct me if I’m wrong but when your Proc returns in a method it immediately moves to the end of that method. Because of that, it will disregard any other code or return commands after it is called… A lambda, however, will go back to the method and run all the code following the “your_lambda.call” command.

Furthermore, when using a Proc you are probably aware that you can pass arguments into it ex: “|your_argument|”. Regardless of whether you ever call that argument in your Proc, it will still run normally. A Lambda, on the other hand, requires any arguments passed into it to be used. Hopefully this helps. You can go here for more information.

2 Likes

The lesson says that it will throw an error if you pass in the wrong number of arguments, not if you don’t use the passed arguments. Is this a quirk the lesson doesn’t explain?

Yes the lesson should explain this better because an error is thrown for both.

I don’t understand the point of calling the lambda when It will not even run its code, and simply goes to the “puts Ironman will win”? Can someone please explain the usefulness here?

6 Likes

Could somebody explain why “Batman will win!” is not printed in the batman_ironman_lambda method even though the lambda here is called? Thanks!

1 Like

I was confused by the same thing. Here’s what I think is happening: it’s not that the lambda code did not execute, but that only the last evaluated expression in the method gets implicitly returned. In the case of the proc however, it actually skips over the rest of the method. No idea why it would skip over the rest of the method though; I’m guessing that’s not how you usually use it.

1 Like

Procs are less picky about the number of args, but in neither case is there an error if an argument is unused. Sample code:

foo = Proc.new {|x| 3}
bar = lambda {|x| 4}

foo.call()  # no error
bar.call()  # error, wrong number of arguments

foo.call(7)  # no error
bar.call(8)  # no error

foo.call(7, 8)   # no error; the extra arg is ignored
bar.call(7, 8)   # error, wrong number of arguments

I don’t quite follow your question exactly (Not a fault of your post. My thinking has become muddled these days). If you clarify your reasoning a bit more, perhaps that might clear things further.
The original snippet of the exercise is:

def batman_ironman_proc
  victor = Proc.new { return "Batman will win!" }
  victor.call
  "Iron Man will win!"
end

puts batman_ironman_proc

def batman_ironman_lambda
  victor = lambda { return "Batman will win!" }
  victor.call
  "Iron Man will win!"
end

puts batman_ironman_lambda

In the snippet, we first define the method batman_ironman_proc and then we call it via the statement:
puts batman_ironman_proc
When the method is executed, a Proc is created and assigned to victor. Then victor is called and the string “Batman will win!” is returned immediately exiting the method batman_ironman_proc. Since we had used the statement puts batman_ironman_proc, so the returned string shows up on the screen.

Moving further along the snippet, we then define a method called batman_ironman_lambda and then we call it via the statement:
puts batman_ironman_lambda
When the method is executed, a Lambda is created and assigned to victor. Then victor is called. The string “Batman will win!” is returned to the calling method batman_ironman_lambda but we don’t do anything with this returned string. We aren’t assigning it to a variable or using puts. Nothing is being done to the returned string. So, we move on to the last statement of the method “Iron Man will win!”. Since it is the last statement of the method, so this string gets implicitly returned from the method batman_ironman_lambda and since we had used puts when making the call puts batman_ironman_lambda, so the string “Iron Man will win!” is shown on the screen.

Perhaps try making this edit and see what happens:

def batman_ironman_proc
  victor = Proc.new { return "Batman will win!" }
  abc = victor.call
  puts abc
  "Iron Man will win!"
end

puts batman_ironman_proc

def batman_ironman_lambda
  victor = lambda { return "Batman will win!" }
  abc = victor.call
  puts abc
  "Iron Man will win!"
end

puts batman_ironman_lambda

The output you will see is:

Batman will win!
Batman will win!
Iron Man will win!

Why?
In the proc method, as soon as the victor.call is executed, the string “Batman will win!” is returned and we exit the method immediately skipping doing anything else in the method.
In the lambda method, victor.call is executed and the returned string “Batman will win!” is assigned to abc. Then puts abc displays this string. Finally, the last string “Iron Man will win!” is implicitly returned and because of our call puts batman_ironman_lambda, it too is displayed.

If I have made things more convoluted, then hopefully this example from shoaib is more succinct and illustrative: What's the difference between a proc and a lambda in Ruby? - Stack Overflow

3 Likes

Thank you very much for taking time to respond to my question. I appreciate your effort to help. But let me clarify my question.

When I run the code in the original snippet, i.e. puts batman_ironman_lamda, the output is:

Batman will win!
Iron Man will win!

This doesn’t make sense to me. There are no print statements (puts) within the code, only at the outside, so the only thing that should be printed is what is returned by batman_ironman_lamba, which (I thought) should be the last thing, i.e. the string “Iron Man will win!” I don’t understand why “Batman will win!” gets printed!

Does that clarify the reason for my confusion?

1 Like

Oh wait, I totally overlooked the puts batman_ironman_proc statement on line 7!!! Doh!

To quote Emily Litella (sp?): “Never mind.” :-]

1 Like

thank you for the detailed explanation and going through the code one by one, was hoping for a comment like this :heart_eyes:

1 Like

Much appreciation for you explanation! I understand the difference now :+1:

1 Like