Skip to content

Instantly share code, notes, and snippets.

@pinzolo
Last active January 2, 2016 16:19
Show Gist options
  • Save pinzolo/8329636 to your computer and use it in GitHub Desktop.
Save pinzolo/8329636 to your computer and use it in GitHub Desktop.
直前のアイテムを保持した each。each_cons(2) だと処理内で現在のアイテム、直前のアイテムの固定化ができない。
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