Skip to content

Loading…

Adds support for Enumerator return when #each is called without a block #120

Merged
merged 1 commit into from

2 participants

@ssimeonov

No description provided.

@TylerBrock

Great, thank you @ssimeonov!

@TylerBrock TylerBrock merged commit b51ca06 into mongodb:master
@gjmurakami-10gen gjmurakami-10gen added a commit that referenced this pull request
@gjmurakami-10gen gjmurakami-10gen Ruby 1.8.7 "fix" for Merge pull request #120 from Shopximity/ss_cursor
 Adds support for Enumerator return when #each is called without a block
b1790d7
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Showing with 15 additions and 4 deletions.
  1. +11 −4 lib/mongo/cursor.rb
  2. +4 −0 test/cursor_test.rb
View
15 lib/mongo/cursor.rb
@@ -276,7 +276,7 @@ def batch_size(size=nil)
end
# Iterate over each document in this cursor, yielding it to the given
- # block.
+ # block, if provided. An Enumerator is returned if no block is given.
#
# Iterating over an entire cursor will close it.
#
@@ -287,11 +287,18 @@ def batch_size(size=nil)
# puts doc['user']
# end
def each
- while doc = self.next
- yield doc
+ if block_given?
+ while doc = self.next
+ yield doc
+ end
+ else
+ Enumerator.new do |yielder|
+ while doc = self.next
+ yielder.yield doc
+ end
+ end
end
end
-
# Receive all the documents from this cursor as an array of hashes.
#
# Notes:
View
4 test/cursor_test.rb
@@ -93,6 +93,10 @@ def test_explain
assert_kind_of Numeric, explaination['nscanned']
end
+ def test_each_with_no_block
+ assert_kind_of Enumerator, @@coll.find().each
+ end
+
def test_count
@@coll.remove
Something went wrong with that request. Please try again.