Last active
January 2, 2016 16:19
-
-
Save pinzolo/8329636 to your computer and use it in GitHub Desktop.
直前のアイテムを保持した each。each_cons(2) だと処理内で現在のアイテム、直前のアイテムの固定化ができない。
This file contains hidden or 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_prev(first_prev = nil) | |
return to_enum(:each_with_prev, first_prev) unless block_given? | |
prev = first_prev | |
each do |item| | |
yield(item, prev) | |
prev = item | |
end | |
self | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment