Reduce consecutive integers in an array into ranges.
Add this line to your application's Gemfile:
gem 'reduce_ranges'
And then execute:
$ bundle
Or install it yourself as:
$ gem install reduce_ranges
ReduceRanges adds an extension to the Array object. To use it, call #reduce_ranges
on an array of integers:
[-3, -2, -1, 1, 3, 4, 5].reduce_ranges #=> [-3..-1, 1, 3..5]
Note that this only works on arrays of integers. If you call it on an array with any other kind of element, it will raise an error:
%w(what it do).reduce_ranges #=> TypeError
[1.0, 2.0, 3.0].reduce_ranges #=> TypeError
# etc..
Shout outs to @calyhre for the current implementation and @lkdjiin for his blog posts on the subject!i
- Fork it ( https://github.com/jacobaweiss/reduce_ranges/fork )
- Create your feature branch (
git checkout -b my-new-feature
) - Commit your changes (
git commit -am 'Add some feature'
) - Push to the branch (
git push origin my-new-feature
) - Create a new Pull Request