Sorry for the late reply.  Apparently your posting didn't make it to
the news group (yet).  I start getting really annoyed by this half
working gateway.

2006/2/22, Chris Pine <chris / pine.fm>:
> > > 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?
>
> Yep.  On the next page (Chapter=11), I talk about, among other things,
> yield.  But I wanted to bring it in later as another way of doing it.

Ah, I see.

> > 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).
>
> Default way to go for an experienced programmer to use?  Or for a
> non-programmer to learn?  Very different situations.  :)

I can only speak from my experience: I learned yield earlier and the
only thing I found difficult about it that I couldn't figure how to
pass a block on to some other method. I knew there had to be a way but
the manual forwarding with a block seemed to awkward... :-)

> > 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.
>
> Well, there's "anonymous" (like &block), and then there's *invisible*,
> like what yield is calling.  There's a big difference.  I'm just
> trying to make it visible, which is an important step on the way to
> understandable.
>
> Plus, passing a function around, and calling a function... these
> things are extensions of what the reader has already learned at that
> point.  "yield" is something totally new.  Better to describe this in
> terms of something the reader is more familiar with first; then I can
> just say "yield is a magical shortcut".  Because it sort of it, right?
>  It's magical, and magical is (among other things) weird.  That's my
> hypothesis, anyway.  :)

Sounds quite reasonable.  I never looked at it this way - and never
experienced similar problems (probably because I never gave a Ruby
class :-)).

The way I'd do it is explain if you have a method that accepts a block
use yield to invoke it. Yield is like a function call, arguments are
passed on to the block and the block's return value is returned from
yield. Oh, and btw. if you want to store that block, pass it on to
another method or change it's scope with instance_eval you can access
it directly by adding &block at the end of the method's argument list
like this: <example>

> In any case, the PickAxe does it the other way, so hopefully we have
> every learning style covered.  :)

Now we just need some magic that will direct novices to the source
appropriate for their learning style. :-))

Kind regards

robert


--
Have a look: http://www.flickr.com/photos/fussel-foto/