Class: Array

Inherits:
Object
  • Object
show all
Defined in:
lib/core_ext/array.rb

Instance Method Summary collapse

Instance Method Details

#consolidate_ranges(method = :itself) ⇒ Array

Consolidate array of possibly overlapping ranges.

Examples:

[15..17, 7..11, 12..13, 8..12, 12..13].consolidate_ranges
# => [7..13, 15..17]

Parameters:

  • method (Symbol, nil) (defaults to: :itself)

    to call on range members for comparison

Returns:

  • (Array)

    consolidated array



21
22
23
24
25
26
27
28
29
30
31
32
33
34
# File 'lib/core_ext/array.rb', line 21

def consolidate_ranges(method=:itself)
  uniq.sort_by { [_1.begin, _1.end] }.then do |ranges|
    consolidated, a = [], ranges.first
    Array(ranges[1..]).each do |b|
      if a.end.send(method) >= b.begin.send(method)   # overlapping
        a = (a.begin..(a.end.send(method) > b.end.send(method) ? a.end : b.end))
      else   # not overlapping
        consolidated << a
        a = b
      end
    end
    consolidated << a
  end.compact
end

#constantizeClass, Module

Convert array of namespaces to constant.

Examples:

%i(AIPP AIP Base).constantize   # => AIPP::AIP::Base

Returns:

  • (Class, Module)

    converted array



9
10
11
# File 'lib/core_ext/array.rb', line 9

def constantize
  map(&:to_s).join('::').constantize
end