benny
11/5/2004 1:57:00 PM
benny wrote:
> Harry Ohlsen wrote:
>
>>> -----Original Message-----
>>> From: Wayne Vucenic [mailto:nightphotos@gmail.com]
>>> Sent: Friday, 5 November 2004 12:12
>>> To: ruby-talk ML
>>> Subject: Re: Cleaner way to do this?
>>>
>>> How about
>>>
>>> [1, 2, 3, 4, 5].each_with_index do |x, i|
>>> if i == 0
>>> print x
>>> else
>>> print ", ",x
>>> end
>>> end
>>
>> That's what I initially suggested :-).
>>
>> However, he felt it was a bit crazy to do the test every time, given the
>> large size of the array.
>>
>
> how about this?
>
> arr = [1, 2, 3, 4, 5]
> first = arr.shift
> print arr.inject(first) { | c, i | "#{c.to_s}, #{i.to_s}" }
>
> benny
or even
print [1, 2, 3, 4, 5].join(' ,')
benny