-
-
Save anonymous/c7abf40be7c47c523977 to your computer and use it in GitHub Desktop.
array to range
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
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