93

How do I do this type of for loop in Ruby?

for(int i=0; i<array.length; i++) {

}
mikej
  • 65,295
  • 17
  • 152
  • 131
DNB5brims
  • 29,344
  • 50
  • 131
  • 195

10 Answers10

121
array.each do |element|
  element.do_stuff
end

or

for element in array do
  element.do_stuff
end

If you need index, you can use this:

array.each_with_index do |element,index|
  element.do_stuff(index)
end
Eimantas
  • 48,927
  • 17
  • 132
  • 168
80
limit = array.length;
for counter in 0..limit
 --- make some actions ---
end

the other way to do that is the following

3.times do |n|
  puts n;
end

thats will print 0, 1, 2, so could be used like array iterator also

Think that variant better fit to the author's needs

Ph0en1x
  • 9,943
  • 8
  • 48
  • 97
32

I keep hitting this as a top link for google "ruby for loop", so I wanted to add a solution for loops where the step wasn't simply '1'. For these cases, you can use the 'step' method that exists on Numerics and Date objects. I think this is a close approximation for a 'for' loop.

start = Date.new(2013,06,30)
stop = Date.new(2011,06,30)
# step back in time over two years, one week at a time
start.step(stop, -7).each do |d| 
    puts d
end
dsz
  • 4,542
  • 39
  • 35
23

The equivalence would be

for i in (0...array.size)
end

or

(0...array.size).each do |i|
end

or

i = 0
while i < array.size do
   array[i]
   i = i + 1 # where you may freely set i to any value
end
Nisse Engström
  • 4,738
  • 23
  • 27
  • 42
jsh-c
  • 231
  • 2
  • 5
17
array.each_index do |i|
  ...
end

It's not very Rubyish, but it's the best way to do the for loop from question in Ruby

EmFi
  • 23,435
  • 3
  • 57
  • 68
14

To iterate a loop a fixed number of times, try:

n.times do
  #Something to be done n times
end
yts
  • 1,890
  • 1
  • 14
  • 24
11

If you don't need to access your array, (just a simple for loop) you can use upto or each :

Upto:

2.upto(4) {|i| puts i}
2
3
4 

Each:

(2..4).each {|i| puts i}
2
3
4
MarredCheese
  • 17,541
  • 8
  • 92
  • 91
Benjamin Crouzier
  • 40,265
  • 44
  • 171
  • 236
8

What? From 2010 and nobody mentioned Ruby has a fine for /in loop (it's just nobody uses it):

ar = [1,2,3,4,5,6]
for item in ar
  puts item
end
steenslag
  • 79,051
  • 16
  • 138
  • 171
  • 5
    One advantage of *.each... do* is that the block temporary falls out of scope at the end of the loop, whereas *for...in* leaves the temporary in scope. – Wayne Conrad Aug 16 '13 at 13:44
4
['foo', 'bar', 'baz'].each_with_index {|j, i| puts "#{i} #{j}"}
YOU
  • 120,166
  • 34
  • 186
  • 219
0

Ruby's enumeration loop syntax is different:

collection.each do |item|
...
end

This reads as "a call to the 'each' method of the array object instance 'collection' that takes block with 'blockargument' as argument". The block syntax in Ruby is 'do ... end' or '{ ... }' for single line statements.

The block argument '|item|' is optional but if provided, the first argument automatically represents the looped enumerated item.

JackyJohnson
  • 3,106
  • 3
  • 28
  • 35