Skip to content

Instantly share code, notes, and snippets.

@kkismd
Created September 26, 2013 03:22
Show Gist options
  • Save kkismd/6709486 to your computer and use it in GitHub Desktop.
Save kkismd/6709486 to your computer and use it in GitHub Desktop.
ひとつ先読みするeach
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