From: Robert Klemme Date: 2006-02-19T19:38:29+09:00 Subject: Re: Understanding ruby? need help Chris Pine wrote: > On 2/18/06, bbqbaker wrote: >> is this kind of right?? thanks > > Yep, that's basically it. Coming from C++, I also had a tough time > with blocks at first (but they are so great, so it's worth the > effort). C++ might want to compare blocks with functors. I like to think of them as anonymous functions with a closure. Often the closure is't even needed, such in simple examples where the block only operates on its arguments. > I wrote a tutorial that talks, among other things, about a somewhat > different approach to understanding blocks and procs: > > http://pine.fm/LearnToProgram/?Chapter=10 Chris, I can't see "yield" mentioned on that page. Is it just me, did I overlook it? Is this intentionally? I mean, "yield" would be the first thing that I'd describe on such a page; IMHO it's the default way to go when passing blocks to methods (it's also the fastest AFAIK). I only use &block if I have to store the block in a variable and / or have to pass it on to another (recursive) method call. > and, of course, it's also covered in my book: > > http://www.pragmaticprogrammer.com/titles/fr_ltp/ > > (The book covers what's in the tutorial, but considerably more in > depth, and with the benefit of the feedback I got from the tutorial > (which came first), as well as covering things that never made it into > the tutorial.) > > Essentially, what I found confusing was "yield". Once you look at the > problem from the angle of little functions (like the C++ function you > wrote) that you pass around explicitly, it makes more sense (to me, at > least). > > Once I wrapped my head around that, it wasn't hard to see yield and > the "do |...| code-code-code end" syntax as just shortcuts to make the > whole process easier to program. If you look at the block as anonymous function it's clear that one needs a special syntax to invoke it (can't use the name). That's exactly what "yield" does. > And for the record, I still think "yield" is a weird name for it. :) OTOH this has the advantage of drawing people's attention to it which probably makes reading code easier. :-) Kind regards robert