Welcome to the Treehouse Community

The Treehouse Community is a meeting place for developers, designers, and programmers of all backgrounds and skill levels to get support. Collaborate here on code errors or bugs that you need feedback on, or asking for an extra set of eyes on your latest project. Join thousands of Treehouse students and alumni in the community today. (Note: Only Treehouse students can comment or ask questions, but non-students are welcome to browse our conversations.)

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and a supportive community. Start your free trial today.

Ruby Ruby Foundations Procs & Lambdas Review

Returning from a proc vs. lambda

The correct answer to the question "what does the return keyword do in a lambda?" is "Returns from the lambda", but doesn't the a lambda actually go back to the parent method, from which it was called? Maybe it's the wording of this answer that is confusing me. In the code example from Jason, the returned value was the puts statement in the parent method, not in the lambda. So wouldn't the correct answer be "Returns from the calling method"?

2 Answers

Richard Luick
Richard Luick
10,955 Points

I struggled to understand this at first since the wording was a little confusing but I think I was able to figure it out so I'll do my best to answer (eventhough this post is a couple months old).

I believe he is saying that when return is called in the proc, then that return value is used as the return for the proc and the entire method. Therefore, the rest of the method is not executed after the proc. However, in a lambda, it will return only from the lambda and then continue to execute the rest of the method. Since the rest of the method is executed, the return value that you see is going to be the last line of the method.

That was my interpretation of the video so I hope this helps!

Eric Carey
Eric Carey
6,820 Points

Well, it helped me out. The wording was indeed tricky. I'll add this for anyone who hits this question in the future.

It might help you to add "puts" before variable.call like this:

def return_from_proc
  variable = proc { return "Returning from proc inside method" }
  puts variable.call
  return "Returning from proc as last line from method"
end

def return_from_lambda
  variable = lambda { return "Returning from lambda inside method" }
  puts variable.call
  return "Returning from lambda as last line from method"
end

If you do that, you'll see that in return_from_proc, literally nothing runs after the "proc { return...". In return_from_lambda, however, you get both sentences.

My non-perfect understanding is that this happens because lambdas are essentially methods, but procs are just convenient snippets of code. This is why procs don't throw an error if you pass them an incorrect number of arguments. They aren't methods, so they don't have any of the functionality and checks that you get with a method.

Lambdas on the other hand are methods and so have their own return value, require the right number of arguments, etc. Really you could re-write the return_from_lambda method like this:

def return_from_pseudo_lambda
  def pseudo_lambda
    return "Returning from lambda inside method"
  end
  puts pseudo_lambda()
  return "Returning from lambda as last line from method"
end

puts return_from_pseudo_lambda

You'll get the same output, but you had to write way more code.

So in short, think of lambdas as a kind of anonymous/un-named method that can speed up the writing of your code, whereas procs are simply convenient groups of code.