From: Farrel Lifson Date: 2007-03-06T07:37:37+09:00 Subject: Re: [PATCH] Join with block On 05/03/07, Steven Lumos wrote: > Why bother passing sep to the block? If the block is a pre-existing proc object there's no guarantee it will have access to whatever is passed to the join method as the separator. > Can your implementation handle [1,2,3].join {...} => "1, 2 and 3"? If the elements in the array are unique it's pretty simple: ary = [1,2,3] ary.join{|sep,a,b| !ary.last.eql?(b) ? ", " : " and "} For a case where elements are repeated (but sequences are not): ary=[1,2,1] ary.join{|sep,a,b| !ary.last(2).eql?([a,b]) ? "," : " and "} For more complex cases where sequences are repeated ([1,2,1,2]) you're probably going to need some extra logic: ary=[1,2,1,2];idx = 0 ary.join{|sep,a,b| ((idx+=1) < ary.length-1) ? ", " : " and "} Farrel