If you have a collection, such as an array or a range, you can also use the Ruby each iterator to create a loop. : Array Each « Array « Ruby






If you have a collection, such as an array or a range, you can also use the Ruby each iterator to create a loop.


grades = [88, 99, 73, 56, 87, 64]

sum = 0

grades.each do |grade|
  sum += grade
end

average = sum / grades.length

puts average

 








Related examples in the same category

1.each method goes through each element of the array and passes it as a parameter to the code block you supply.
2.Identify the Array and Hash of Customers