Created
June 22, 2016 10:03
-
-
Save tandibar/919fdc00d87090f2a731496c2b1d192c to your computer and use it in GitHub Desktop.
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
# 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