Great question! It turns out that in Ruby 1.9 or 2.0 it's actually faster to use each
, and not for
(by a similar very small amount).
Looking into why for
is faster than each
in Ruby 1.8.7, it turns out Ruby 1.8.7 uses a slightly different code path for each
vs. for
, although for
still is automatically evaluated as a call to each
internally:
if (nd_type(node) == NODE_ITER) {
result = rb_eval(self, node->nd_iter);
}
else {
VALUE recv;