Skip to content

Instantly share code, notes, and snippets.

@jferris
Created March 3, 2016 16:44
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jferris/460e3a1b7b1ba87953f1 to your computer and use it in GitHub Desktop.
Save jferris/460e3a1b7b1ba87953f1 to your computer and use it in GitHub Desktop.
Combine ranges
def combine_ranges(ranges)
if ranges.empty?
[]
else
first, *rest = ranges
rest.reduce([first]) do |result, current|
previous = result.pop
if previous.max + 1.day >= current.min
result << previous.min..current.min
else
result << previous << current
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment