[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.ruby

yield example baffling - need help

Tom Cloyd

1/15/2009 1:40:00 AM

I'm studying the yield statement for the first time, today.

All's well until I see this example:

def myeach(myarray)
iter = 0
while (iter < myarray.length):
yield(myarray[iter])
iter += 1
end
end

testarray = [1,2,3,4,5]
myeach(testarray) {|item| print "#{item}:"}
â?? 1:2:3:4:5:

I see a method with one parameter. There a block to the right of the
method call, inexplicably. It would make sense if some iteration were
going on, but I don't see that at all. One call, one execution (with a
loop), and we're out. What does "item" have to count through?

How can that yield do anything? How does it know about the block? It's
not passed in. The iteration appears to be happening IN the method,
using a block it cannot possibly see. Ouch! After some minutes, I'm not
getting this at all.

Can anyone clear my head for me. I've never seen anything like this. Or
is it just a weird Ruby idiom I'll just have to accept?

Thanks in advance.

Tom


--

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Tom Cloyd, MS MA, LMHC - Private practice Psychotherapist
Bellingham, Washington, U.S.A: (360) 920-1226
<< tc@tomcloyd.com >> (email)
<< TomCloyd.com >> (website)
<< sleightmind.wordpress.com >> (mental health weblog)
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~


5 Answers

Tom Cloyd

1/15/2009 1:45:00 AM

0

Tom Cloyd wrote:
> I'm studying the yield statement for the first time, today.
>
> All's well until I see this example:
>
> def myeach(myarray)
> iter = 0
> while (iter < myarray.length):
> yield(myarray[iter])
> iter += 1
> end
> end
>
> testarray = [1,2,3,4,5]
> myeach(testarray) {|item| print "#{item}:"}
> â?? 1:2:3:4:5:
>
> I see a method with one parameter. There a block to the right of the
> method call, inexplicably. It would make sense if some iteration were
> going on, but I don't see that at all. One call, one execution (with a
> loop), and we're out. What does "item" have to count through?
>
> How can that yield do anything? How does it know about the block? It's
> not passed in. The iteration appears to be happening IN the method,
> using a block it cannot possibly see. Ouch! After some minutes, I'm
> not getting this at all.
>
> Can anyone clear my head for me. I've never seen anything like this.
> Or is it just a weird Ruby idiom I'll just have to accept?
>
> Thanks in advance.
>
> Tom
>
>
Arrgh. What a dolt. I just realized that ALL the yield examples I've
been studying have this block sitting over to the right - I was
converting the curly braces to parens in my mind, and so didn't see
them. It IS idiomatic weirdness. I yield to higher wisdom.

t.

--

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Tom Cloyd, MS MA, LMHC - Private practice Psychotherapist
Bellingham, Washington, U.S.A: (360) 920-1226
<< tc@tomcloyd.com >> (email)
<< TomCloyd.com >> (website)
<< sleightmind.wordpress.com >> (mental health weblog)
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~


Tom Cloyd

1/15/2009 2:01:00 AM

0

David A. Black wrote:
> Hi --
>
> On Thu, 15 Jan 2009, Tom Cloyd wrote:
>
>> Tom Cloyd wrote:
>>> I'm studying the yield statement for the first time, today.
>>>
>>> All's well until I see this example:
>>>
>>> def myeach(myarray)
>>> iter = 0
>>> while (iter < myarray.length):
>>> yield(myarray[iter])
>>> iter += 1
>>> end
>>> end
>>>
>>> testarray = [1,2,3,4,5]
>>> myeach(testarray) {|item| print "#{item}:"}
>>> â?? 1:2:3:4:5:
>>>
>>> I see a method with one parameter. There a block to the right of the
>>> method call, inexplicably. It would make sense if some iteration
>>> were going on, but I don't see that at all. One call, one execution
>>> (with a loop), and we're out. What does "item" have to count through?
>>>
>>> How can that yield do anything? How does it know about the block?
>>> It's not passed in. The iteration appears to be happening IN the
>>> method, using a block it cannot possibly see. Ouch! After some
>>> minutes, I'm not getting this at all.
>>>
>>> Can anyone clear my head for me. I've never seen anything like this.
>>> Or is it just a weird Ruby idiom I'll just have to accept?
>>>
>>> Thanks in advance.
>>>
>>> Tom
>>>
>>>
>> Arrgh. What a dolt. I just realized that ALL the yield examples I've
>> been studying have this block sitting over to the right - I was
>> converting the curly braces to parens in my mind, and so didn't see
>> them. It IS idiomatic weirdness. I yield to higher wisdom.
>
> Half the fun, actually, is that while you know what you're yielding,
> you never know exactly what you're yielding *to* :-)
>
> The block construct actually has two common use patterns: iterating
> through collections, and yielding "magic pen" objects.[1] An example
> of the magic pen is File.open:
>
> File.open("blah", "w") do |fh|
> fh.puts(whatever)
> end
>
> where the open method, which you never have to see, does the
> housekeeping, including closing the file for you. You just get the
> magic pen (the file handle). That pattern is, I think, particularly
> nice.
>
>
> David
>
> [1] http://dablog.rubypal.com/2007/2/18/the-magic-pe...
>
David - very nice! Plus, you made me howl with laughter. Yes, I often
cannot see to what I am yielding. And of course, we spend a lifetime
approaching the Great Yield. Um...but not tonight, I think!

Thanks for your eloquence.

t.

--

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Tom Cloyd, MS MA, LMHC - Private practice Psychotherapist
Bellingham, Washington, U.S.A: (360) 920-1226
<< tc@tomcloyd.com >> (email)
<< TomCloyd.com >> (website)
<< sleightmind.wordpress.com >> (mental health weblog)
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~


Brian Candler

1/16/2009 1:22:00 PM

0

Tom Cloyd wrote:
> How can that yield do anything? How does it know about the block? It's
> not passed in. The iteration appears to be happening IN the method,
> using a block it cannot possibly see. Ouch! After some minutes, I'm not
> getting this at all.

Maybe it's clearer if you rewrite myeach to show explicitly the block
being passed in, and being called.

def myeach(myarray, &block) # <<<
iter = 0
while (iter < myarray.length):
block.call(myarray[iter]) # <<<
iter += 1
end
end

Even if there is no &foo in the argument list, you can still pass a
block when you call the method. The method can call it anonymously using
'yield', and test for its presence using 'block_given?' (*)

BTW, Proc#[] is an alias for Proc#call, so this gives you three choices:

block.call(args) # only if block captured into a variable
block[args] # ditto
yield args

HTH,

Brian.

(*) It's occasionally a source of head-scratching when you pass a block
to a method which doesn't expect one, and so it's silently ignored. Ruby
can't and doesn't warn you of this.
--
Posted via http://www.ruby-....

Virgil

1/2/2013 12:14:00 AM

0

In article <lgs6e8d0escqreb9ov2ben90melhj2duao@4ax.com>,
duke <duckgumbo32@cox.net> wrote:

> On Wed, 2 Jan 2013 07:37:19 +1100, "Andrew W"
> <remove_ajwerner@optusnet.com.au>
> wrote:
>
> >> Good grief, he doesn't even understand.
>
> >You're the one who's incapable of understanding.
> >You or some moron made up the notion that "God infuses the human soul on the
> >first cell split" (or conception), and like a fool you tried to pass it off
> >as fact in this group.
>
> God revealed it.
>
> >Then you used stupid reasonings like muscle movements to try to support it.
> >Lol.
> >Since when do dividing cells have muscles and move?
>
> I want you to tell me how a fetus at 24 weeks has a beating heart and a
> functioning brain.
>
> >Since when do dividing cells show individual human like nature and
> >demonstrate the presence of a soul?

>
> You walked right into ignorance on that
> one.

The only ignorance around here is duckgumbo's.

And there is no evidence of any living thing, animal or vegetable (or
otherwise, if any) having any sort of soul, at least of the type that
duckgumbo hypothesize.
--


Andrew W

1/2/2013 2:33:00 AM

0

"duke" <duckgumbo32@cox.net> wrote in message
news:lgs6e8d0escqreb9ov2ben90melhj2duao@4ax.com
> On Wed, 2 Jan 2013 07:37:19 +1100, "Andrew W"
> <remove_ajwerner@optusnet.com.au> wrote:
>
>>> Good grief, he doesn't even understand.
>
>> You're the one who's incapable of understanding.
>> You or some moron made up the notion that "God infuses the human
>> soul on the first cell split" (or conception), and like a fool you
>> tried to pass it off as fact in this group.
>
> God revealed it.
>

Rubbish. Prove it.
Some religious dope clearly made it up.
You haven't been able to provide any support for it what so ever. Just bald
faced claims.

>
>> Then you used stupid reasonings like muscle movements to try to
>> support it. Lol.
>> Since when do dividing cells have muscles and move?
>
> I want you to tell me how a fetus at 24 weeks has a beating heart and
> a functioning brain.
>

Same way animals do. Genetic propagation.
Any more dumb questions?

>
>> Since when do dividing cells show individual human like nature and
>> demonstrate the presence of a soul?
>> Dope.
>
> But you're too stupid to understand. You walked right into ignorance
> on that one.
>

What's a matter duke? Couldn't answer the question with anything intelligent
yet again?


--
Religions breed hypocrisy and self-righteousness.

Many Christians spend more time looking down on other people than up
towards Christ.