C++:
for(i=0,j=0;i<0;i++,j++)
What's the equivalence to this in ruby?
Besides the normal for
, while
loop seen in C++. Can someone name off the other special loops ruby has? Such as .times
? .each
?
Thanks in advance.
C++:
for(i=0,j=0;i<0;i++,j++)
What's the equivalence to this in ruby?
Besides the normal for
, while
loop seen in C++. Can someone name off the other special loops ruby has? Such as .times
? .each
?
Thanks in advance.
Ruby is different to C++. In C++ you use a for loop to loop through anything, but in Ruby you'll find you're usually looping through an enumerable object, so it's more common to do something like:
monkeys.each do |monkey|
monkey.say 'ow!'
end
Don't try to look for too much equivalence between the two languages - they're built for different things. Obviously there are a lot of equivalent things, but you can't learn Ruby by producing a chart that shows C++ code on one side and the Ruby equivalent on the other. Try to learn the idiomatic way of doing things and you'll find it much easier.
If you want ways of looping through enumerable objects, check out all the methods in Module: Enumerable
: all? any? collect detect each_cons each_slice each_with_index entries enum_cons enum_slice enum_with_index find find_all grep include? inject inject map max member? min partition reject select sort sort_by to_a to_set zip
. With most of these methods you'd use a for
loop to do the equivalent thing in C++.
I am not terribly familiar with C++, but AFAICS, the equivalent Ruby code to the loop you posted is simply:
i, j = 0, 0
Which shows once again the expressive power Ruby has. Anybody can figure out what this does, even if he has never seen Ruby before, while the equivalent C++ takes quite a while to figure out.
If I understand your question (at least the first part of it), you are wondering how you can iterate two separate variables at the same time, such as i
and j
.
You can do that in Ruby using the for
loop, with multiple variables. For instance, if you wanted i
to count up from 1 to 10, and j
to count from 10
to 20
, you could do:
for i, j in (1..10).zip(10..20)
puts "#{i}, #{j}"
end
zip
will produce, from two arrays, a single array of which each element is an array, with the first element taken from the corresponding position in the first array, and the second element taken from the corresponding position in the second array:
> [1, 2, 3].zip([4, 5, 6])
=> [[1, 4], [2, 5], [3, 6]]
And using i, j
in your for
loop will take i
from the first element of each inner array, and j
from the second element.
If you'd rather use each
than for
, you can just use a block with two parameters:
(1..10).zip(10..20).each { |i, j| puts "#{i}, #{j}" }
As to the second part of your question, Ruby doesn't really have a fixed number of different iterators, since most iteration is done by passing a block to a method, and thus any class can define its own methods that allow iterating over its own contents. The most common is each
, and any class that defines an each
method can mix in the Enumerable
class, which gives you a variety of different methods for iterating over elements, selecting elements, filtering, and so on. There are also times
, upto
, and downto
defined on the Integer
class, each_key
, each_value
, each_pair
on Hash
, each_byte
, each_char
, each_line
on String
, and so on. Just about any class that defines some sort of collection or sequence has methods for iterating over said collection or sequence.