Are equivalent.
for a in b; code end
is a syntax sugar for b.each {|a| code }
, with the difference that the variable a
does not have its scope limited to the block. A simple proof for this is as follows:
class A
def each(&block)
block.call(1)
block.call(2)
block.call(42)
end
end
for x in A.new
p x
end
# mostra 1, 2 e 42
p x # mostra 42
# a variável ainda está viva aqui, é a única diferença
There was a difference in performance in that the for
was less efficient than the each
, but this is of little significance from the Ruby 2.0.
As a reference, part of compile.c
:
// Ao encontrar um "for"
if (nd_type(node) == NODE_FOR) {
COMPILE(ret, "iter caller (for)", node->nd_iter);
// Crie um bloco para ele
iseq->compile_data->current_block =
NEW_CHILD_ISEQVAL(node->nd_body, make_name_for_block(iseq),
ISEQ_TYPE_BLOCK, line);
// Crie uma invocação ao método "each" passando o bloco
ADD_SEND_R(ret, line, ID2SYM(idEach), INT2FIX(0),
iseq->compile_data->current_block, INT2FIX(0));
}
It is worth noting that in ruby it is much more idiomatic and preferable to use the each
in place of for
, but this is more a matter of style than practice.
Nenhuma
anda variável ainda está viva aqui, é a única diferença
contradicts. :-)– fotanus
A failed attempt to short Answer. I reformulated.
– Guilherme Bernal