Skip to content

Instantly share code, notes, and snippets.

Created September 24, 2014 14:54
Show Gist options
  • Save anonymous/c7abf40be7c47c523977 to your computer and use it in GitHub Desktop.
Save anonymous/c7abf40be7c47c523977 to your computer and use it in GitHub Desktop.
array to range
ruby -le '
a = [1,2,4,6,7,9,10,11]
p a.inject([]) {
|val, val1|;
val.size > 0 ? (val[-1].last == val1-1 ? val[-1].push(val1) : val << [val1]; ) : val << [val1];val
}.map { |arr| (arr.first..arr.last) }
'
[1..2, 4..4, 6..7, 9..11]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment