Variable methods only iterate if you provide a block, otherwise they return an iterator that is similar to C ++. For example, in irb :
>> e = [1,2,3,4].each => #<Enumerator: [1, 2, 3, 4]:each> >> e.next => 1
It's hard to say that .next is a lot like e++ in C ++ because it returns the current value and increases the iterator. There is a .rewind method, but it resets the iterator to the beginning, and does not return just one step.
I don’t know about a convenient way to detect the end of an iterator (except for the StopIteration exception) or to determine how large the iterator is.
Presumably, you should grab the iterator, pass it to some method, and the method does iter.each { |x| something_interesting(x) } iter.each { |x| something_interesting(x) } .
So there are iterators, but you cannot transliterate your C ++ directly into Ruby. OTOH, you should not transliterate your C ++ to Ruby, you should write Ruby in Ruby and C ++ in C ++.
source share