From: Mitch Vincent Date: 2000-12-14T03:37:28+09:00 Subject: [ruby-talk:7168] Re: || notation? Excellent. Thanks very much... Oh and the book is on it's way as we speak :-) -Mitch On Thu, 14 Dec 2000, Dave Thomas wrote: > Mitch Vincent writes: > > > Thread.start(@listen.accept) do |aConnection| > > > > Ok, I'll take a guess here but I want to make sure i'm right and that I > > understand this.. > > > > Does the above assign the returning value of @listen.accept to the > > variable aConnection (Which is more or less a socket descriptor)? I > > suppose that's pretty obvious, as that's how it does seem to work in the > > rest of the code, I've just never seen an assignment made like that.. > > And a good guess it is. That's exactly the effect, but perhaps not > quite for the reason you might expect. > > The |...| notation is used with blocks. A block in Ruby is a chunk of > code that is remembered, not executed, at the point of definition. In > a way, a block is link an anonymous subroutine, except a block has > some additional magic we don't need to think about here ;-) > > If a block is like a subroutine, then the stuff between the ||'s are > the block's parameters. So, in the example above, 'aConnection' is a > parameter for the block that follows the Thread.start. > > So, how you you call one of these blocks? It's pretty > simple. A block can only be defined by associating it with a method > invocation. Within that method, there's a special statement, 'yield', > which calls back out to the block. You can pass parameters to yield, > which are then assigned to the block's formal parameters. > > So, Thread.start starts the corresponding block as a new thread, and > passes any parameters given to Thread.start on to the block > itself. Why does it go through this level of indirection? > > You'll have to buy the book ;-) > > > > Dave >