I like Martin's implementation. I've modified it so that it handles an empty
collection.

#--
module Enumerable
  def each_with_last
    prev = nil
    empty = true
    each { |item|
      if empty
        empty = false
      else
        yield prev,false
      end
      prev = item
    }
    if not empty
      yield prev,true
    end
  end
end

def format(a)
  result = "["
  a.each_with_last {|i, last|
    result += i.to_s
    if not last
      result += ","
    end
  }
  result += "]"
end

puts(format([1,2,3,4,5,6,7,8,9]))
puts(format([1,2]))
puts(format([1]))
puts(format([]))
#--

Since the flag variable ('empty' here) is necessary to detect the empty
collection, the use of each_with_index as in other posts was unnecessary.

Steve.