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

10 Responses Follows

  1. Daniel says

    Thanks for this. Very helpful!

  2. David Rivers says

    Why didn’t you bring this to my attention 20 minutes ago in that phone interview!

  3. Troy Goode says

    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?

  4. Nimesh Nikum says

    Really great findings and thanks for this :)

  5. Neelesh says

    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…..

  6. Matt says

    @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.

  7. beats by dr dre says

    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.

  8. beats by dr dre says

    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.

  9. John says

    Thanks for providing the clear insight on the difference between these two

  10. John says

    I prefer lambda, mainly because it’s more like a function if I called f() { return 1; } it wouldn’t be returned.


Your Response