Skip to content

Instantly share code, notes, and snippets.

@tandibar
Created June 22, 2016 10:03
Show Gist options
  • Save tandibar/919fdc00d87090f2a731496c2b1d192c to your computer and use it in GitHub Desktop.
Save tandibar/919fdc00d87090f2a731496c2b1d192c to your computer and use it in GitHub Desktop.
# patch array for pairwise function
class Array
def pairwise
pairs = []
a = self.slice(1..-1).each_slice(2).to_a
self.each_slice(2).to_a.each_with_index do |el, idx|
pairs << el if el.size > 1
pairs << a[idx] if a[idx] && a[idx].size > 1
end
pairs.each do |pair|
yield(pair[0], pair[1])
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment