ryanprayogo
ryanprayogo

Reputation: 11817

Is there a Ruby version of for-loop similar to the one on Java/C++?

Is there a Ruby version of for-loop similar to the one in Java/C(++)?

In Java:

for (int i=0; i<1000; i++) {
    // do stuff
}

The reason is because I need to do different operations based on the index of the iteration. Looks like Ruby only has a for-each loop?

Am I correct?

Upvotes: 11

Views: 7489

Answers (9)

draegtun
draegtun

Reputation: 22560

How about step?

0.step(1000,2) { |i| puts i }

is equivalent to:

for (int i=0; i<=1000; i=i+2) {
    // do stuff
}

Upvotes: 11

Sagiv Ofek
Sagiv Ofek

Reputation: 25280

for i in 0..100 do
  #bla bla
end

Upvotes: 2

Munish
Munish

Reputation: 988

The while loop executes its body zero or more times as long as its condition is true.

while <condition>
    # do this
end

The while loop can substitute the 'for' loop of Java. In Java,

for (initialization;, condition;, incrementation;){
    //code 
}

is same as following (except, in the second form, initialized variables are not local to for-loop).

initialization;
for(, condition;, ) {
    //code
    incrementation;
}

The ruby 'while' loop can be written in this form to work as for-loop of Java. In Ruby,

initialization;
while(condition)
    # code
    incrementation;
end 

Note that the 'while' (and 'until' and 'for') loop doesn't introduce a new scope; previously existing locals can be used in the loop, and new locals created will be available afterwards.

Upvotes: 7

horseyguy
horseyguy

Reputation: 29895

when i just need the numbers (and not wanting to iterate) I prefer this:

(0..10000).each do |v|
    puts v
end

Upvotes: 1

user214028
user214028

Reputation: 203

Ruby tends to use iterators rather than loops; you can get all the function of loops with Ruby's powerful iterators.

There are several choices to do this, let's assume you have an array 'arr' of size 1000.

1000.times {|i| puts arr[i]}
0.upto(arr.size-1){|i| puts arr[i]}
arr.each_index {|i| puts arr[i]}
arr.each_with_index {|e,i| puts e} #i is the index of element e in arr

All these examples provide the same functionality

Upvotes: 15

TK.
TK.

Reputation: 28153

times is recommended over each_with_index. times is around 6 times faster. Run the code below.

require "benchmark"

TESTS = 10_000_000
array = (1..TESTS).map { rand }
Benchmark.bmbm do |results|
  results.report("times") do
    TESTS.times do |i|
      # do nothing
    end
  end

  results.report("each_with_index") do
    array.each_with_index do |element, index|
      # Do nothing
    end
  end
end

I got the result as below with my MacBook (Intel Core2Duo).

Rehearsal ---------------------------------------------------
times             1.130000   0.000000   1.130000 (  1.141054)
each_with_index   7.550000   0.210000   7.760000 (  7.856737)
------------------------------------------ total: 8.890000sec

                      user     system      total        real
times             1.090000   0.000000   1.090000 (  1.099561)
each_with_index   7.600000   0.200000   7.800000 (  7.888901)

Upvotes: 1

erik
erik

Reputation: 564

In Ruby, the for loop may be implemented as:

1000.times do |i|
  # do stuff ...
end

If you want both the element and the index then the each_with_index syntax is probably best:

collection.each_with_index do |element, index|
  # do stuff ...
end

However the each_with_index loop is slower since it provides both the element and index objects for each iteration of the loop.

Upvotes: 5

Waseem
Waseem

Reputation: 8402

You can user each with index.

Upvotes: 1

nas
nas

Reputation: 3696

Yes you can use each_with_index

collection = ["element1", "element2"]
collection.each_with_index {|item,index| puts item; puts index}

the 'index' variable gives you the element index during each iteration

Upvotes: 12

Related Questions