On Apr 1, 2006, at 7:32 PM, Benjohn Barnes wrote:

>
> On 1 Apr 2006, at 18:27, James Edward Gray II wrote:
>
>> On Apr 1, 2006, at 11:20 AM, Benjohn Barnes wrote:
>>
>>> => 'tomato, cheese, ham and pineapple'
>>>
>>> Is something I'd like to be able to do, and I almost thought it's  
>>> be part of the standard. Is there already a standard way of  
>>> getting that (facets, perhaps?)? Any chance it'll make it in to  
>>> the standard Enumerable?
>>
>> >> list = %w{tomato cheese ham pineapple}
>> => ["tomato", "cheese", "ham", "pineapple"]
>> >> [list[0..-2].join(", "), list[-1]].join(", and ")
>> => "tomato, cheese, ham, and pineapple"
>>
>> Hope that helps.
>>
>> James Edward Gray II
>
> ! :) Thanks!
>
> Argh, and it almost works in the edge cases, except for when  
> there's 0 or 1 item in the list.
>
> a = [1]
> => [1]
> irb(main):030:0> [a[0..-2].join(', '), a[-1]].join(' and ')
> => " and 1"
> irb(main):031:0> a = []
> => []
> irb(main):032:0> [a[0..-2].join(', '), a[-1]].join(' and ')
> => " and "
>
> When I was looking at this earlier, I found that..
>
> def a_function(arg1, arg2 = arg1)
> 	puts arg1, arg2
> end
>
> ...worked as I'd expect, and was surprised that join didn't take an  
> optional second argument to be the separator between the last two  
> items.
>
> Cheers,
> 	Benj
>
>

This works in all the edge cases also, but its a bit verbose:
class Array
   def multi_join(*args)
     if args.empty?
       join
     elsif args.length == 1
       join(args[0])
     else
       accum = ""
       normal = args[0]
       last = args[1]
       index_of_second_to_last_item = length - 2
       index_of_last_item = length - 1
       each_with_index do |item, count|
         accum << item.to_s
         if count == index_of_last_item
           break
         elsif count == index_of_second_to_last_item
           accum << last
         else
           accum << normal
         end
       end
       accum
     end
   end
end