In python I can get an iterator from any iterable with iter(); and then I can call next(my_iter) to get the next element.
Is there any equivalent in ruby/rails?
In python I can get an iterator from any iterable with iter(); and then I can call next(my_iter) to get the next element.
Is there any equivalent in ruby/rails?
On
There are many iterators in Ruby as follows:
Each Iterator: This iterator returns all the elements of an array or a hash. Each iterator returns each value one by one. Syntax:
collection.each do |variable_name|
# code to be iterate
continue if condition # equivalent next in python
end
In the above syntax, the collection can be the range, array or hash.
referenced from https://www.geeksforgeeks.org/ruby-types-of-iterators/
.to_enumwill yield the enumerator. For an examplea.to_enumwill yield the enumerator and you can iterate it from there likea.to_enum.each{|x| p x}.Or without loop, you can take the element like