Ruby Each Loop

Ruby Each Loop

The Ruby Each loop statement syntax and example.

Each loop syntax

expression.each do value
	-- Ruby code 
end

Each loop example

[1,2,3,4,5].each do |x|
   puts("The value of x = #{x}" )
end

Output

The value of x = 1

The value of x = 2

The value of x = 3

The value of x = 4

The value of x = 5