Permalink
Browse files

fix code samples in iterator docs

  • Loading branch information...
1 parent 1829c0f commit 50def352c24431fe2bf68885b9845f8768636d35 @tmm1 tmm1 committed Sep 5, 2011
Showing with 42 additions and 42 deletions.
  1. +42 −42 lib/em/iterator.rb
View
@@ -4,46 +4,46 @@ module EventMachine
# Unlike ruby's built-in iterators, the end of the current iteration cycle is signaled manually,
# instead of happening automatically after the yielded block finishes executing. For example:
#
- # (0..10).each{ |num| }
+ # (0..10).each{ |num| }
#
# becomes:
#
- # EM::Iterator.new(0..10).each{ |num,iter| iter.next }
+ # EM::Iterator.new(0..10).each{ |num,iter| iter.next }
#
# This is especially useful when doing asynchronous work via reactor libraries and
# functions. For example, given a sync and async http api:
#
- # response = sync_http_get(url); ...
- # async_http_get(url){ |response| ... }
+ # response = sync_http_get(url); ...
+ # async_http_get(url){ |response| ... }
#
# a synchronous iterator such as:
#
- # responses = urls.map{ |url| sync_http_get(url) }
- # ...
- # puts 'all done!'
+ # responses = urls.map{ |url| sync_http_get(url) }
+ # ...
+ # puts 'all done!'
#
# could be written as:
#
- # EM::Iterator.new(urls).map(proc{ |url,iter|
- # async_http_get(url){ |res|
- # iter.return(res)
- # }
- # }, proc{ |responses|
- # ...
- # puts 'all done!'
- # })
+ # EM::Iterator.new(urls).map(proc{ |url,iter|
+ # async_http_get(url){ |res|
+ # iter.return(res)
+ # }
+ # }, proc{ |responses|
+ # ...
+ # puts 'all done!'
+ # })
#
# Now, you can take advantage of the asynchronous api to issue requests in parallel. For example,
# to fetch 10 urls at a time, simply pass in a concurrency of 10:
#
- # EM::Iterator.new(urls, 10).each do |url,iter|
- # async_http_get(url){ iter.next }
- # end
+ # EM::Iterator.new(urls, 10).each do |url,iter|
+ # async_http_get(url){ iter.next }
+ # end
#
class Iterator
# Create a new parallel async iterator with specified concurrency.
#
- # i = EM::Iterator.new(1..100, 10)
+ # i = EM::Iterator.new(1..100, 10)
#
# will create an iterator over the range that processes 10 items at a time. Iteration
# is started via #each, #map or #inject
@@ -70,17 +70,17 @@ def concurrency=(val)
# Iterate over a set of items using the specified block or proc.
#
- # EM::Iterator.new(1..100).each do |num, iter|
- # puts num
- # iter.next
- # end
+ # EM::Iterator.new(1..100).each do |num, iter|
+ # puts num
+ # iter.next
+ # end
#
# An optional second proc is invoked after the iteration is complete.
#
- # EM::Iterator.new(1..100).each(
- # proc{ |num,iter| iter.next },
- # proc{ puts 'all done' }
- # )
+ # EM::Iterator.new(1..100).each(
+ # proc{ |num,iter| iter.next },
+ # proc{ puts 'all done' }
+ # )
#
def each(foreach=nil, after=nil, &blk)
raise ArgumentError, 'proc or block required for iteration' unless foreach ||= blk
@@ -136,13 +136,13 @@ class << on_done
# Collect the results of an asynchronous iteration into an array.
#
- # EM::Iterator.new(%w[ pwd uptime uname date ], 2).map(proc{ |cmd,iter|
- # EM.system(cmd){ |output,status|
- # iter.return(output)
- # }
- # }, proc{ |results|
- # p results
- # })
+ # EM::Iterator.new(%w[ pwd uptime uname date ], 2).map(proc{ |cmd,iter|
+ # EM.system(cmd){ |output,status|
+ # iter.return(output)
+ # }
+ # }, proc{ |results|
+ # p results
+ # })
#
def map(foreach, after)
index = 0
@@ -174,14 +174,14 @@ def next
# Inject the results of an asynchronous iteration onto a given object.
#
- # EM::Iterator.new(%w[ pwd uptime uname date ], 2).inject({}, proc{ |hash,cmd,iter|
- # EM.system(cmd){ |output,status|
- # hash[cmd] = status.exitstatus == 0 ? output.strip : nil
- # iter.return(hash)
- # }
- # }, proc{ |results|
- # p results
- # })
+ # EM::Iterator.new(%w[ pwd uptime uname date ], 2).inject({}, proc{ |hash,cmd,iter|
+ # EM.system(cmd){ |output,status|
+ # hash[cmd] = status.exitstatus == 0 ? output.strip : nil
+ # iter.return(hash)
+ # }
+ # }, proc{ |results|
+ # p results
+ # })
#
def inject(obj, foreach, after)
each(proc{ |item,iter|

0 comments on commit 50def35

Please sign in to comment.