Ruby Ruby Loops Ruby Iteration Iteration With Each

Eylon Cohen
Eylon Cohen
4,779 Points

Quick Question about loops and iterations

Why a change in variables inside an .each iteration does not change the variable in the original array, while a simillar change inside a loop does change the original array?

Thank you!

1 Answer

Jorge Rodriguez
Jorge Rodriguez
2,615 Points

Hello Eylon, the each method won't change the values of the original array, it will only iterate into the array, meanwhile there is another method called: "map" what it does is to create a new array containing the values returned by the block.

Attached you will find the documentation of ruby:

hope it is helpful!