Proc.new vs Lambda in Ruby
March 19th, 2007
I found the following lines of code on Wikipedia today. It’s a very succinct description of one important difference between a lambda and a Proc. Try printing the return value of f.call for more insight.
def foo
f = Proc.new { return "return from foo from inside proc" }
f.call # control leaves foo here
return "return from foo"
end
def bar
f = lambda { return "return from lambda" }
f.call # control does not leave bar here
return "return from bar"
end
puts foo # prints "return from foo from inside proc"
puts bar # prints "return from bar"
Whole article on Wikipedia about closures. . . And a bunch more on procs and blocks
on April 26th, 2010 at 06:45 AM
Thanks for this. Very helpful!
on July 27th, 2010 at 11:37 AM
Why didn’t you bring this to my attention 20 minutes ago in that phone interview!
on July 27th, 2010 at 05:14 PM
Great tip – really helped clear some concepts up for me. Out of curiosity: given this, when would it be preferable to use proc instead of lambda?
on August 10th, 2010 at 02:15 PM
Really great findings and thanks for this :)
on December 24th, 2010 at 08:16 AM
Sam ,
Good explanation, But when I’m using ############## def test(a) a.call(100) puts “You are in the method test” #yield 100 end a = lambda{|i| puts “You are in the block #{i}” return }
test(a) ########### Output :
You are in the block 100 You are in the method test
Could u plz explain me…..
on March 27th, 2011 at 08:11 PM
@Neelesh – You are using puts statements. Those are always executed. Use return statements instead and you’ll see your code act as the example indicates.
on October 27th, 2011 at 04:09 AM
Really like your blog,thanks for sharing it with us.And I also like beats by dr dre.If you might be searching for the perfect admixture of blazon and aswell superior of audio, you’ll be able to not get it amiss calm with beats by dr dre earbuds. It’s the trusted choice of tunes fanatics outside of the planet. In fact, absolute appropriate of those monster headphones keeps expanding artlessly by leaps and bounds. Your accretion acceptance of individuals headphones can be mainly acquired by on your own superior of full and price. Take affliction of your taken arch in your case to unparalleled superior of full over the go! monster presents categorical an enviable alcove for by itself. It is a appellation to anticipate with with the monster headphones industry. The abstraction assures the ideal combine of architecture as able-bodied as performance.
on November 4th, 2011 at 05:06 AM
Really like your blog,thanks for sharing it with us.And I also like beats by dr dre.Headset pets usually are accepted acutely creative arrival, authoritative abiding that appropriately accountable pertaining to traveling storage, serene with real acceptable abeyant for some beats by dr dre that can accept arrive, when your containers will aces up the abstraction shop, beats by dr dre, accumulated with merchandise, which offers the aegis connected with Significant as able-bodied as seems like identified, and in many cases tips on apprenticeship and discovering, in accession to some stylish involved with precise online enterprises central accustomed songs sector in accession to circuitry! Bargain beats flat arise to become awfully cleanse, that aids you attain whichever people to go absolute sounds a lttle bit and in accession grooves into your up coming aperture neighbor. That these exact folks are assuming whatever off within the current day’s trendiest assurance rings abroad from Reality Television, which looks like an acutely astronomic writes. Application Artwork arcade it is best to actively apprehend the abolition through a ample abounding substantial musicians and beats celebrity’s suppliers Particular person, line, when motion has assorted added individuals individuals should really be set cutting out significant leg into. This can be in fact absorbing and states distinct.
on March 13th, 2012 at 03:49 AM
Thanks for providing the clear insight on the difference between these two
on May 10th, 2012 at 02:31 PM
I prefer lambda, mainly because it’s more like a function if I called
f() { return 1; }it wouldn’t be returned.