From: Steven Lumos Date: 2007-03-06T10:44:44+09:00 Subject: Re: [PATCH] Join with block "Farrel Lifson" writes: > 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. Okay, but why does that matter? Hopefully I'm not just using an overly simple, but why is something like [1,3,2].join(' (X) ') do |sep,a,b| sep.sub('X', a < b ? 'less' : 'greater or equal') end preferable to [1,3,2].join {|a,b| a < b ? ' (less) ' : ' (greater or equal) '} >> 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 After reading the archives, I think there are too many conflicting opinions about what a block argument to #join means. *I* think it should help with certain common cases for English where currently you have type a lot more code to get something that ought to (IMO) be simple. Your implementation on the other hand focuses on handling complex relationships between elements, and doesn't help me at all. :-) I wouldn't prefer your example to something like: a = [1,2,3] "#{a[0..-2].join(', ')} and #{a[-1]}" #=> "1, 2 and 3" But then the previous discussions seem to be more interested in combining #map with #join, although there definitely seems to be greater interest in your problem domain than mine. So what's missing is a convincing argument for which of these cases deserve special treatment and why. I consider my case to be a small special case within the traditional domain of #join. But I'm also sympathetic to arguments that however close, it's still outside the domain. Steve