Control Flow

For Each in Ruby

The for-each loop iterates through every element in the array, without specifying size.


Syntax
array.each do |element|
    #code to perform, on element
end

Notes

Each element is assigned a temporary iterator variable, declared in the for-each structure.

Elements are not accessed in any particular order.


Example
grades.each do |grade|
    puts "Student grade: {grade}"
end

See Also
Documentation

Introduction to Ruby - Loops

Add to Slack

< For Loop   |   While Loop >

© 2019 SyntaxDB. All Rights Reserved.