Check out example codes for "ruby for loop". It will help you in understanding the concepts better.
Code Example 1
# You can use an each to mimic the functionality of a for loop
(0..5).each do |i|
puts "Inside loop i = #{i}"
# put your code for loop here
end
# outputs
# Inside loop i = 0
# Inside loop i = 1
# Inside loop i = 2
# Inside loop i = 3
# Inside loop i = 4
# Inside loop i = 5
Code Example 2
for element in array do
element.do_stuff
end
Code Example 3
for i in 0..2 do
print(i)
end
# => 0
# => 1
# => 2
Code Example 4
while $i < $num do
puts("Inside the loop i = #$i" )
$i +=1
end
Code Example 5
for element in array do
element.do_stuff
end
Code Example 6
#!/usr/bin/ruby
$i = 0
$num = 5
while $i < $num do
puts("Inside the loop i = #$i" )
$i +=1
end
# outputs
# Inside the loop i = 0
# Inside the loop i = 1
# Inside the loop i = 2
# Inside the loop i = 3
# Inside the loop i = 4
Learn ReactJs, React Native from akashmittal.com