From: Zach Dennis Date: 2004-11-05T13:21:48+09:00 Subject: Re: Cleaner way to do this? Zach Dennis wrote: > > If your friend knows the processing he wants to do on the first > element, why not just keep it simple... > > > arr = [1,2,3,4,5] > process = proc{ |e| print e }.call( arr[0] ) > arr[1...arr.length].each do |e| > print ", #{e}" > end > This is also faster, then doing a comparison in each iteration : RESULTS: ---------- Capture Output ---------- "C:\ruby\bin\ruby.exe" -d harry_ohlsen.rb WRITING 1 Took: 19.297000169754 seconds WRITING 2 Took: 11.7650001049042 seconds Terminated with exit code 0 --------CODE----------- require 'time' arr = [] 2000000.times do |i| arr << "abcdefghijklmnopqrstuvwxyz" end f = File.new( "temp.txt" , "w+" ) t = Time.new arr.each_with_index do |x,i| if i==0 f.print x else f.print ', ',x end end f.close puts "WRITING 1" puts "Took: ", (Time.new.to_f - t.to_f).to_s, " seconds" f = File.new( "temp.txt2" , "w+" ) t = Time.new process = proc { |e| f.print e }.call( arr[0] ) arr[1...arr.length].each do |e| f.print ', ',e end f.close puts "WRITING 2" print "Took: ", (Time.new.to_f- t.to_f).to_s, " seconds"