Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Working examples of EM::Iterator

Does anyone have any working examples of EM::Iterator? The only examples I can find seem to be copies of (or point back to):

http://yardoc.org/docs/eventmachine-eventmachine/EventMachine/Iterator

I don't see any instances of EM::Iterator in EventMachine's Rdoc, so I'm not sure if it's an old class that has been removed or not. I generally get the following error when I try to use EM::Iterator:

NameError: uninitialized constant EventMachine::Iterator

Thanks!

like image 410
cmhobbs Avatar asked Aug 02 '10 03:08

cmhobbs


People also ask

What is iterator explain with example?

An Iterator is an object that can be used to loop through collections, like ArrayList and HashSet. It is called an "iterator" because "iterating" is the technical term for looping. To use an Iterator, you must import it from the java.util package.

How does an iterator work?

An iterator is an object (like a pointer) that points to an element inside the container. We can use iterators to move through the contents of the container. They can be visualised as something similar to a pointer pointing to some location and we can access content at that particular location using them.

How do I write an iterator?

Obtain an iterator to the start of the collection by calling the collection's iterator( ) method. Set up a loop that makes a call to hasNext( ). Have the loop iterate as long as hasNext( ) returns true. Within the loop, obtain each element by calling next( ).

What are Iterables and iterators?

An Iterable is basically an object that any user can iterate over. An Iterator is also an object that helps a user in iterating over another object (that is iterable). Method Used. We can generate an iterator when we pass the object to the iter() method. We use the __next__() method for iterating.


1 Answers

The problem is that the latest released EventMachine version is 0.12.10, which is now 10 months old. EM::Iterator appears to have been added to the code base after that release; to utilize it, you will need to be using the development version of EventMachine.

The following worked for me:

$ git clone git://github.com/eventmachine/eventmachine.git
$ cd eventmachine
$ irb -Ilib -reventmachine
irb(main):001:0> EM.run do
irb(main):002:1*   EM::Iterator.new(0..10, 2).each do |num, iter|
irb(main):003:2*     puts num
irb(main):004:2>     EM.add_timer(1) { iter.next }
irb(main):005:2>   end
irb(main):006:1> end
...

There are also some slides relating to EM::Iterator in Aman Gupta's excellent presentation EventMachine: scalable non-blocking i/o in ruby, pages 46-50.

like image 82
Arto Bendiken Avatar answered Sep 27 '22 15:09

Arto Bendiken