Class: RangeList
- Includes:
- Enumerable
- Defined in:
- lib/quality_extensions/range_list.rb
Overview
A RangeList is a mixed array of numbers and ranges.
It can be used to to represent “discontiguous ranges” (something a single Range object cannot do, unfortunately), for example [1..3, 5, 7..9],
RangeList([1..3, 5, 7..9]).to_a
=> [1, 2, 3, 5, 6, 7]
A RangeList acts like a range:
-
iterators like each will yield each element from a range (expanding them to the array of elements they represent) rather than the range itself RangeList([1..2, 4]).map {|e| e} # => [1, 2, 4]
-
to_a expands any ranges in the RangeList using expand_ranges and returns a simple array composed of its atomic elements and elements from the expanded ranges
In every other respect, however, a RangeList behaves like a range.
Defined Under Namespace
Classes: FormatError
Class Method Summary collapse
Instance Method Summary collapse
- #each ⇒ Object
-
#expand_ranges ⇒ Object
(also: #to_a)
Converts to a normal array, expanding all Ranges contained in this array, replacing the range with the list of elements that the range represents (range.
to_a
) . -
#initialize(*args) ⇒ RangeList
constructor
A new instance of RangeList.
-
#to_range_list ⇒ Object
end.
Methods included from Enumerable
#all_same?, #every, #gradiate, #grep_indexes, #grep_plus_offset, #grep_with_index, #grep_with_regexp_support_for_symbols, #group_by_and_map, #map_with_index, #map_with_index!, #max_by_value, #reject_with_index, #reject_with_index!, #select!, #select_from, #select_until, #select_until_last, #select_until_last_with_index, #select_until_with_index, #select_while, #select_while_with_index, #select_with_index, #select_with_index!
Methods inherited from Array
#after, #average, #before, #classify, #delete_if!, #extract_options!, #group_by, #ljust_columns, #ljust_columns!, #ljust_rows, #ljust_rows!, #mean, #rjust_columns, #rjust_columns!, #rjust_rows, #rjust_rows!, #select_if!, #select_if_with_index!, #shell_escape, #sum, #to_a_recursive, #to_query_string
Constructor Details
Class Method Details
Instance Method Details
#each ⇒ Object
72 73 74 |
# File 'lib/quality_extensions/range_list.rb', line 72 def each .each { |element| yield element } end |
#expand_ranges ⇒ Object Also known as: to_a
Converts to a normal array, expanding all Ranges contained in this array, replacing the range with the list of elements that the range represents (range.to_a
) .
57 58 59 60 61 62 63 64 65 66 67 68 69 |
# File 'lib/quality_extensions/range_list.rb', line 57 def new_array = [] @array.each do |item| silence_warnings do # Object.to_a: warning: default `to_a' will be obsolete if item.respond_to?(:to_a) new_array.concat item.to_a else new_array.concat [item] end end end new_array end |
#to_range_list ⇒ Object
end
51 52 53 |
# File 'lib/quality_extensions/range_list.rb', line 51 def to_range_list self end |