Created
October 12, 2013 16:31
-
-
Save korny/6951979 to your computer and use it in GitHub Desktop.
Simplistic backport of Enumerable#slice_before from Ruby 1.9 to Ruby 1.8.
This file contains 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
class Array | |
def slice_before | |
[].tap do |chunks| | |
each do |item| | |
chunks << [] if yield(item) || chunks.empty? | |
chunks.last << item | |
chunks | |
end | |
end | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Here's the documentation.