Le dimanche 25 fñ×rier 2007 03:15, S. Robert James a ñÄrit > Thanks for the excellent explanation and code. > > One question I have is: why is each_cons inconsistent vis-a-vis > each_slice? each_slice returns the trailing items, padded with nils - > why doesn't each cons do the same? > Hmm, are you sure of that ? The last object yielded by each_slice is an array containing the trailling items, but not padded with nils : irb(main):004:0> (1..10).each_cons(3) {|i| p i} [1, 2, 3] [2, 3, 4] [3, 4, 5] [4, 5, 6] [5, 6, 7] [6, 7, 8] [7, 8, 9] [8, 9, 10] => nil irb(main):005:0> (1..10).each_slice(3) {|i| p i} [1, 2, 3] [4, 5, 6] [7, 8, 9] [10] => nil Of course, if you try to access the elements of the array individually, you will get the default value for the "empty" indices : nil. irb(main):006:0> (1..10).each_slice(3) {|i| p [i[0], i[1], i[2]]} [1, 2, 3] [4, 5, 6] [7, 8, 9] [10, nil, nil] => nil There's no inconsistency, then. I hope I answered your question :) -- Olivier Renaud