benjohn
4/2/2006 12:32:00 AM
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