Created
September 26, 2013 03:22
-
-
Save kkismd/6709486 to your computer and use it in GitHub Desktop.
ひとつ先読みするeach
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
module Enumerable | |
def each_with_peek_ahead | |
@_start = false | |
self.each_with_index do |current, idx| | |
if idx == 0 | |
@_last = current | |
@_start = true | |
else | |
yield @_last, current | |
@_last = current | |
end | |
end | |
if @_start | |
yield @_last, nil | |
end | |
end | |
end | |
def test_ewpa(array) | |
puts "test with an Array #{ array.inspect }" | |
array.each_with_peek_ahead do |current, ahead| | |
puts "current: #{current}, next: #{ahead}" | |
end | |
end | |
test_ewpa([]) | |
test_ewpa([1]) | |
test_ewpa([1,2]) | |
test_ewpa([1,2,3]) | |
test_ewpa([1,2,3,4]) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment