Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

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,616 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: https://ruby-doc.org/core-2.2.0/Array.html#method-i-map

hope it is helpful!

Greetings!