back to listing index vs Lambda in Ruby by Sam

[web search]
Original source (
Clipped on: 2012-08-30 vs Lambda in Ruby

March 19th, 2007

Posted by sam in Programming, Ruby

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 for more insight.

 def foo f = { return "return from foo from inside proc" } # control leaves foo here return "return from foo" end def bar f = lambda { return "return from lambda" } # 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

13 Responses Follows

  1. Daniel says
    on April 26th, 2010 at 06:45 AM

    Thanks for this. Very helpful!

  2. David Rivers says
    on July 27th, 2010 at 11:37 AM

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

  3. Troy Goode says
    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?

  4. Nimesh Nikum says
    on August 10th, 2010 at 02:15 PM

    Really great findings and thanks for this :)

  5. Neelesh says
    on December 24th, 2010 at 08:16 AM

    Sam ,

    Good explanation, But when I’m using ############## def test(a) 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
    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.

  7. beats by dr dre says
    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.

  8. beats by dr dre says
    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.

  9. John says
    on March 13th, 2012 at 03:49 AM

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

  10. John says
    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.

  11. beasfunsc says
    on June 22nd, 2012 at 08:59 AM – 266ft clamp meter инструкция по измерению тока – amiko hd-8300”руководство – 1с бухгалтерия 8.2. инструкция – ввод сотрудников – aria soho pdf инструкция – aristo jzs160 инструкция – ariston bfs1221 инструкция – a-4 2001года инструкция к автомагнитоле – 3g модем sierra wireless compass 597 инструкция по пользованию – a.p.s 5200 инструкция – adobe acrobat x pro инструкция – aeg telecar d 902 инструкция – 2,4 д аминосоль гербицид инструкция применения – amx 5200i руководство – 2ндфл за 2011г инструкция по заполнению – anaconda a530 руководство – amazon kindle 3 инструкция – ai 858 ctx инструкция – aiwa cdc-r504mp инструкция gj ‘rcgkefnfwbb – adobe dreamweaver cs4 руководство – 3d руководство по ремонту калина 1118 – adobe_audition инструкция по пользованию – ascensia elite инструкция – alligator 425 сигнализация инструкция – 108r00909 заправка инструкция – 1055t разгон в биосе 1801 инструкция – ariston microgenus plus 31 mffi инструкция – alligator инструкция скачать – avidemux 2.5.6 инструкция на русском языке – atomy f198hd (1080p) инструкция – algae destroyer advanced применение инструкция – at-mc102xl инструкция – 720h инструкция – aristo 147 кузов мануал скачать – ariston mba 2185 инструкция – aerofit 8600tm скачать инструкцию – asx штатное головное устройство инструкция – 601 двигатель мерседес руководство по ремонту – ariston awm 108 инструкция на русском – ariston avd 109 инструкция – aeg 11220 инструкция – 28 инструкция об открытии и закрытии банковских счетов – 7,62 игра руководство – alligator скачать инструкцию – 76-мм пушка обр. 1942 г. руководство службы – a<v da-971 gps инструкция настройка блютуз – 1с предприятие управление небольшой фирмой, руководство пользователя скачать бесплатно – ariston aqsf 105 инструкция – alpine cda-9854r инструкция – ab ipbox 200s инструкция – alpine cda 9883r инструкция по эксплуатации

  12. aempirei says
    on July 23rd, 2012 at 07:37 AM

    lol you ‘prefer’ lambda, that’s like you say you prefer addition over subtraction.

  13. Bishnu Pandey says
    on August 24th, 2012 at 04:10 AM

    Thanks for providing clear insight.

Your Response

Name (required)

Mail (will not be published)