The reason why your code does not output all the squares for each value of the array, is because your puts sum is outside the array, thus only printing the last value of sum. The corrected code should look like this
my_array = [1, 2, 3, 4, 5]
sum = 2
my_array.each do |n|
sum = n * n
puts sum
end