Skip to content

Instantly share code, notes, and snippets.

@petelacey
Last active October 15, 2015 19:18
Show Gist options
  • Save petelacey/78c964b924e479678d15 to your computer and use it in GitHub Desktop.
Save petelacey/78c964b924e479678d15 to your computer and use it in GitHub Desktop.
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