By making use of an ellipsis or similar symbols the representation of a list of elements can be abbreviated. This can be helpful in situtations where character space is limited, and at the same time may be more aesthetically pleasing.
Installation as usual:
gem install ellipsis
After you require the gem with
require 'ellipsis'
The array class is extended with the following two methods that are similar to the to_sentence
method in Rails.
This method assumes that the elements represent a sorted list; Using the method displays an ellipsis. For example:
[1,2,3,4,5].to_abbr_series # => "1, 2, ..., 5"
This method summarizes consecutive subsets in with dash. See the following example:
[1,2,3, 5,6].to_abbr_set # => "1-3, 5, 6"
So far the notion of consecutive refers to integer successors. Thus the array needs to be an integer array. If you don’t want integers in the actual string representation you can pass an additional block that will convert an integer to a custom string representation:
WEEKDAYS = ['mon', 'tue', 'wed','thu', 'fri', 'sat', 'sun'] days = [1,2,3,5,6] days.to_abbr_set {|e| WEEKDAYS[e] } # => 'mon-wed, fri, sat'
-
refactor unit tests
-
Implement options similar to the
to_sentence
method in Rails.