ruby iterate over array
# Iterate over an array using each method
array = [1, 2, 3, 4, 5]
array.each do |element|
puts element * 2
end
Explanation:
array
is initialized as an array containing integers from 1 to 5.- The
each
method is called on thearray
. - For each element in the
array
, denoted by the block parameterelement
: - It multiplies the current element by 2.
- The result is then output using
puts
to display it on the console.