Welcome to the Treehouse Community
Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.
Looking to learn something new?
Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.
Start your free trialLorinda Jackson
3,727 PointsProcs and lambdas
I am stuck on this badge. I don't understand how the lambda is returning "the last line of the method". It appears to me that the lambda's value is the one right beside it. Is this because it is returning the last value in the method? I realize the lambda will not return a value if it is called. I'm also confused about the return method and why we need two of them in this method, I assume it's to show us how the code is being read.
def return_from_lambda
variable = lambda {return "a"}
variable.call
return "b"
end
puts return_from_lambda
=>b
Can someone explain how this code is executing? Thanks!
5 Answers
Michael Wilson
3,866 PointsI know this question is quite old now, but in case others run across it...
From the example in the video it looks like invoking a proc (via .call) is what I would term in-line code in other languages. That is, the proc is a short-cut way to add some lines of code at the same level/context. That is why the 'return' inside the proc ends the method. It is just like those lines of code from the proc were typed into the method, so when a return is encountered the method returns.
The lambda is more like a method call inside another method, so it dives into its own context and processes its code then returns to the outer method code. Thus, when a return is encountered in the lambda code it just exits the lambda, not the outer method.
Aaron Daub
30 PointsA lambda and a block are two very different constructs. A lambda is a representation of a method whereas a block (more formally, a Proc (which is short for process)) is a representation of a snippet of code, a process.
Since a lambda is a method in its own right, it has its own return value and its own scope. A Proc does not have its own return value and if it contains a return statement the Proc will cause the enclosing method to return.
For more information: http://www.robertsosinski.com/2008/12/21/understanding-ruby-blocks-procs-and-lambdas/
Aimee Knight
9,701 Points@Lorinda,
I see your confusion also. The video transcript says "while if you use a lambda it will return only from the lambda." I may not have the proper terminology, but the examples seems to show that when using a proc inside a method, returns will be executed by the proc. When using a lambda though, return statements will only be executed by the method (not the lambda). Can anyone watch this video (Programming Ruby Foundations > Procs & Lambdas > Working With Procs & Lambdas) and clarify this for us? As far as the example, I'd also assume the double return statements are only for used as an example.
@Aaron,
Thanks for your link also. At first glance I understand your comments and what the link is showing. I'm having a hard time equating that to the example in the video though. I'll keep trying to work through it, but hopefully someone at Treehouse can take a look.
Aimee Knight
9,701 PointsUpdate. I think I may understand what the video is trying to say. If you use the return keyword in a proc, the proc's return statement will run, and will also stop the containing method execution. If you use the return keyword in a lambda, the lambda will return, and the containing method will continue execution. Is this correct? Any further clarification would still be awesome.
Unsubscribed User
3,309 PointsI actually found this Forum post very helpful (especially the code example): https://teamtreehouse.com/forum/returning-from-a-proc-vs-lambda