back to listing index

Proc.new vs Lambda in Ruby by Sam

[web search]
Original source (samdanielson.com)
Clipped on: 2012-08-30

Proc.new 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 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

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) 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
    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

    http://edamariz1980.pop3.ru/fiperifo.html – 266ft clamp meter инструкция по измерению тока http://edamariz1980.pop3.ru/pirecynyxo.html – amiko hd-8300”руководство http://edamariz1980.pop3.ru/bisyrisy.html – 1с бухгалтерия 8.2. инструкция – ввод сотрудников http://edamariz1980.pop3.ru/maqyqige.html – aria soho pdf инструкция http://edamariz1980.pop3.ru/cedifocy.html – aristo jzs160 инструкция http://edamariz1980.pop3.ru/mafefogote.html – ariston bfs1221 инструкция http://edamariz1980.pop3.ru/felekygi.html – a-4 2001года инструкция к автомагнитоле http://edamariz1980.pop3.ru/moletaqo.html – 3g модем sierra wireless compass 597 инструкция по пользованию http://edamariz1980.pop3.ru/betiwa.html – a.p.s 5200 инструкция http://edamariz1980.pop3.ru/bowemehebo.html – adobe acrobat x pro инструкция http://edamariz1980.pop3.ru/holodixe.html – aeg telecar d 902 инструкция http://edamariz1980.pop3.ru/rywony.html – 2,4 д аминосоль гербицид инструкция применения http://edamariz1980.pop3.ru/kylodihi.html – amx 5200i руководство http://edamariz1980.pop3.ru/mowinini.html – 2ндфл за 2011г инструкция по заполнению http://edamariz1980.pop3.ru/sotepe.html – anaconda a530 руководство http://edamariz1980.pop3.ru/ziforibo.html – amazon kindle 3 инструкция http://edamariz1980.pop3.ru/xopibike.html – ai 858 ctx инструкция http://edamariz1980.pop3.ru/serynopi.html – aiwa cdc-r504mp инструкция gj ‘rcgkefnfwbb http://edamariz1980.pop3.ru/bynima.html – adobe dreamweaver cs4 руководство http://edamariz1980.pop3.ru/netoze.html – 3d руководство по ремонту калина 1118 http://edamariz1980.pop3.ru/tyxeny.html – adobe_audition инструкция по пользованию http://edamariz1980.pop3.ru/megilyge.html – ascensia elite инструкция http://edamariz1980.pop3.ru/razirywoka.html – alligator 425 сигнализация инструкция http://edamariz1980.pop3.ru/domeledali.html – 108r00909 заправка инструкция http://edamariz1980.pop3.ru/qoxiba.html – 1055t разгон в биосе 1801 инструкция http://edamariz1980.pop3.ru/fywepome.html – ariston microgenus plus 31 mffi инструкция http://edamariz1980.pop3.ru/kyxakefe.html – alligator инструкция скачать http://edamariz1980.pop3.ru/gywixiko.html – avidemux 2.5.6 инструкция на русском языке http://edamariz1980.pop3.ru/ciwinofi.html – atomy f198hd (1080p) инструкция http://edamariz1980.pop3.ru/fateky.html – algae destroyer advanced применение инструкция http://edamariz1980.pop3.ru/mehihipana.html – at-mc102xl инструкция http://edamariz1980.pop3.ru/kehefoso.html – 720h инструкция http://edamariz1980.pop3.ru/locicyry.html – aristo 147 кузов мануал скачать http://edamariz1980.pop3.ru/ceqolame.html – ariston mba 2185 инструкция http://edamariz1980.pop3.ru/rifotelo.html – aerofit 8600tm скачать инструкцию http://edamariz1980.pop3.ru/xirelyme.html – asx штатное головное устройство инструкция http://edamariz1980.pop3.ru/qefomifi.html – 601 двигатель мерседес руководство по ремонту http://edamariz1980.pop3.ru/kogice.html – ariston awm 108 инструкция на русском http://edamariz1980.pop3.ru/xexane.html – ariston avd 109 инструкция http://edamariz1980.pop3.ru/copilyhyqo.html – aeg 11220 инструкция http://edamariz1980.pop3.ru/ketyhafyna.html – 28 инструкция об открытии и закрытии банковских счетов http://edamariz1980.pop3.ru/tohosofe.html – 7,62 игра руководство http://edamariz1980.pop3.ru/lyhimecito.html – alligator скачать инструкцию http://edamariz1980.pop3.ru/fifinisika.html – 76-мм пушка обр. 1942 г. руководство службы http://edamariz1980.pop3.ru/qerisizyri.html – a<v da-971 gps инструкция настройка блютуз http://edamariz1980.pop3.ru/foqenyko.html – 1с предприятие управление небольшой фирмой, руководство пользователя скачать бесплатно http://edamariz1980.pop3.ru/todypiha.html – ariston aqsf 105 инструкция http://edamariz1980.pop3.ru/dopozecihe.html – alpine cda-9854r инструкция http://edamariz1980.pop3.ru/wanihigopy.html – ab ipbox 200s инструкция http://edamariz1980.pop3.ru/fehani.html – 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)

Website