On Sat, May 31, 2014 at 1:22 AM, Andrew Vit <andrew / avit.ca> wrote:
> On 14-05-30, 16:13, Matthew Kerwin wrote:
>>
>> Intentional. The function in #reduce is binary (2 in => 1 out), but when
>> there is only one item being reduced (i.e. an empty list & an initial
>> memo, or a single-item list & no memo) what would you pass to the binary
>> function?
>
> Right on. When I thought about it some more I realized the same thing.

There is a distinction between invoking #reduce or #inject without
arguments and with one argument. A structured test will convey that:

irb(main):006:0> 4.times {|i| a=i.times.to_a; printf "%d %p\n",i,a;
a.reduce {|*x| printf ">> %p\n", x;nil}}
0 []
1 [0]
2 [0, 1]
>> [0, 1]
3 [0, 1, 2]
>> [0, 1]
>> [nil, 2]
=> 4
irb(main):007:0> 4.times {|i| a=i.times.to_a; printf "%d %p\n",i,a;
a.reduce(99) {|*x| printf ">> %p\n", x;nil}}
0 []
1 [0]
>> [99, 0]
2 [0, 1]
>> [99, 0]
>> [nil, 1]
3 [0, 1, 2]
>> [99, 0]
>> [nil, 1]
>> [nil, 2]
=> 4

Kind regards

robert

-- 
[guy, jim].each {|him| remember.him do |as, often| as.you_can - without end}
http://blog.rubybestpractices.com/