Skip to content
Browse files

Merge pull request #1347 from jtdowney/enumerator-peek-fixes

Bug fix and spec for Enumerator#peek
  • Loading branch information...
2 parents 4da2d72 + 06188b4 commit 6d1f079d0e792fda5606de65c81c8d622366a822 @rue rue committed Oct 23, 2011
View
9 kernel/common/enumerator.rb
@@ -158,15 +158,6 @@ def reset
end
end
end
-
- # Rewinds the enumeration sequence by the next method.
- #
- # If the enclosed object responds to a "rewind" method, it is called.
- #
- def rewind
- @generator.rewind if @generator
- self
- end
end
end
View
9 kernel/common/enumerator18.rb
@@ -33,5 +33,14 @@ def next
raise StopIteration, "iteration reached end"
end
+
+ # Rewinds the enumeration sequence by the next method.
+ #
+ # If the enclosed object responds to a "rewind" method, it is called.
+ #
+ def rewind
+ @generator.rewind if @generator
+ self
+ end
end
end
View
10 kernel/common/enumerator19.rb
@@ -73,6 +73,16 @@ def peek
item
end
+ # Rewinds the enumeration sequence by the next method.
+ #
+ # If the enclosed object responds to a "rewind" method, it is called.
+ #
+ def rewind
+ @generator.rewind if @generator
+ @lookahead = []
+ self
+ end
+
# A supporting class for Enumerator that allows for easy proxying to a Generator's yield.
class Yielder
def initialize(&block)
View
9 spec/ruby/shared/enumerator/rewind.rb
@@ -29,4 +29,13 @@
@enum.next.should == 1
end
+ ruby_version_is "1.9" do
+ it "works with peek to reset the position" do
+ @enum.next
+ @enum.next
+ @enum.rewind
+ @enum.next
+ @enum.peek.should == 2
+ end
+ end
end

0 comments on commit 6d1f079

Please sign in to comment.
Something went wrong with that request. Please try again.