Hi --

On Wed, 11 Jun 2008, David Flanagan wrote:

> David Flanagan wrote:
>>> ary.each.accumulating(0) {|i, a| a += i}
>>> 
>>> martin
>>> 
>>> 
>> 
>> Martin,
>> 
>> The method does not work like inject: the return value of the block does 
>> not become the new value of the "memo" argument.  If you try the example 
>> you propose using with_memo as it is implemented now, it returns 0, not the 
>> sume of the array elements. That's one reason I think that with_memo is a 
>> bad name: it confuses people to think this is a variant of inject.
>> I think most uses of this method will pass a mutable object as the argument 
>> in order to be able to alter the value.
>>
>>     David
>> 
>
> To follow-up on my previous message, perhaps the name "with_constant" or 
> "with_const" would be helpful to relate this method to "with_index" and also 
> to emphasize that the "memo" value passed is always the same object, and not 
> the accumulated value returned by the last invocation of the block.

"constant" is already a pretty dedicated term, though. with_object
might be better.

> I'm beginning to feel that the method we're discussing here is one particular 
> member of a set of methods for defining custom iterations. Imagine something 
> like:
>
> a1.each.with_index.and_next_of(a2).and_const(a3).and_returning([]) do
> |elt1, index, elt2, a3, retval|
> end
>
> Or:
>
> [1,2,3].each.alternating_with([4,5,6]).and_returning([]) {|x,ret|...}
>
> Does anyone like the use of "and" instead of "with" to indicate the addition 
> of a new value to be passed to the block?

I don't particularly. But then again, I'm not a fan of lots of
chaining that tries to make things sound like English narrative. It
never really does, and I just lose track of it in the Ruby part of my
brain.  That's why, as much as I respect the talent and effort behind
it, I can't use RSpec.


David

-- 
Rails training from David A. Black and Ruby Power and Light:
   INTRO TO RAILS         June 9-12            Berlin
   ADVANCING WITH RAILS   June 16-19           Berlin
   ADVANCING WITH RAILS   July 21-24           Edison, NJ
See http://www.rubypal.com for details and updates!