Ruby Ruby Loops Ruby Iteration Iteration With Each

Thomas Minnick
Thomas Minnick
2,539 Points

whats the difference between what Jason has in the video and this code?

1 In the video he has the code 2 array = [0, 1, 2, 3, 4, 5] 3 array.each do |item| 4 item = item + 2 5 puts "The current item + 2 is #{item}." 6 end

But on like 4 he has item = item + 2

would it be doing the same thing if I were to put it as

item += 2

or would this have a different meaning entirely?

1 Answer

Yes, it is the same. When you come up with this kind of questions, the best thing is actually to try it and see what happens, being curious and trying things is a great way to learn the little details. Cheers.

Thomas Minnick
Thomas Minnick
2,539 Points

I did try it, but I feared maybe it was similar to puts and print where they are completely different but for what I was using them for they showed up the same. Thanks for the answer!