+ 14

Can anyone explain me for loops in Ruby

23rd Mar 2017, 12:25 PM
4 Answers
+ 16
Try to repeat SoloLearn's Ruby course, chapter Control Structures > For loops. If you have further questions, please tell us exactly what your problem is.
23rd Mar 2017, 4:47 PM
Tashi N
Tashi N - avatar
+ 13
24th Mar 2017, 9:44 AM
+ 7
A for loop is like a while loop spot the difference here: while loop: x = 0 while x < 10 x += 1 puts x end for loop: x = 0 for x in 100 x += 1 puts x end I'm not sure to my answer but I think this is right
24th Mar 2017, 9:04 AM
MrCoder - avatar
+ 6
@MrCoder there are other differences, as for iterates over something. e.g. you can say for i in (5..100) puts i end this puts out the numbers 5-100 because it iterates over the range (5..100) you can also iterate over an array, for example arr = ["ThingA", "ThingB", "ThingC"] for i in arr puts i end will output: ThingA ThingB ThingC basically, for reassigns i to the next element of the array/range/hash/whatever each time the loop repeats. when it reaches the end, it stops.
24th Mar 2017, 2:23 PM
Supersebi3 - avatar