There's a thread in ruby-talk about for versus #each.

In Ruby 1.8 for calls #each on the collection and conceptually both
kind of loops are roughly equivalent except for scopes. They run at
comparable speeds in Ruby 1.8.6-p114 as well.

Joe Wlfel noticed there's a significant difference in speed in 1.9
though (I removed the rehearsals):

fxn@feynman:~/tmp$ ruby for_vs_each.rb
                   user     system      total        real
for              1.360000   0.000000   1.360000 (  1.366464)
each             1.580000   0.010000   1.590000 (  1.597240)

fxn@feynman:~/tmp$ ~/ruby-1.9/bin/ruby for_vs_each.rb
                   user     system      total        real
for              2.390000   0.010000   2.400000 (  2.408057)
each             0.660000   0.000000   0.660000 (  0.659654)

Why?

-- fxn


fxn@feynman:~/tmp$ cat for_vs_each.rb
require 'benchmark'

ntimes = 500
a = Array.new(10_000, 1)

Benchmark.bmbm do |bench|
 bench.report "for" do
  x = 0
  ntimes.times do
    for i in a
      x += i
    end
  end
 end

 bench.report "each" do
  x = 0
  ntimes.times do
    a.each do |i|
      x += i
    end
  end
 end
end