Last active
October 15, 2015 19:18
-
-
Save petelacey/78c964b924e479678d15 to your computer and use it in GitHub Desktop.
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
module CoreExtensions | |
module Array | |
module Enhancements | |
# a = [0, 1, 2, 3, 4] | |
# a.splice(2, 1, *[9, 8, 7]) | |
# => [1, 2, 9, 8, 7, 4] | |
def splice(start, len, *replace) | |
self[start, len] = replace | |
self | |
end | |
# a = [1, 2, 3, 4] | |
# a.accumulate | |
# => [1, 3, 6, 10] | |
def accumulate | |
# Don't coerce nils to zero with #to_i. Value may be a float or BigDecimal | |
self.inject([]) { |cs, i| cs << (i || 0) + (cs.last || 0) } | |
end | |
end | |
end | |
end | |
Array.include CoreExtensions::Array::Enhancements |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment