From: Albert Wagner Date: 2001-06-03T02:49:32+09:00 Subject: [ruby-talk:16171] Re: Block arguments vs method arguments > There're TWO closely related but different problems. > > (a) The semantics of block parameters are defined by multiple > assignment. I don't know what this means. That yield calls a block multiple times? > This is because they are originally iteration variables. > But they are sometimes used/expected like formal > arguments (e.g. name shadowing, default value, etc.). But Isn't such use a violation of the defined intent of a block? I always thought of a block as pure code with any/all input supplied by the receiver. Therefore the receiver dictates the contents of |...| to the sender. Essentially, the receiver offers a contract: (a) with ".each" he says "I am iterating over a sequence; I will give your block one object from the sequence at a time; in your block, you tell me what to do with this one item, |a|, (b) with ".sort" he says "I am sorting a collection of objects; I will give your block two objects at a time; in your block, you tell me how you want them ordered; here are the two objects |a b|. > > Plus, |a| accepts any number of values, where |a,| takes only > one value (this too is caused by multiple assignment semantics). I guess I don't understand this either. > > (b) blocks introduce new local variable scope, i.e. a local variable > that appears first in a block is not available out of the > block. This is confusing sometimes. Seems straightforward to me. Why is it confusing? Isn't the block just a piece of helper code for the receiver? Does the sender expect to renegotiate the offered contract on the fly? I suppose I am too new to Ruby to even be commenting. But explaining my understanding of a thing and then being corrected by those that know better, is usefull to my understanding.