Class: Pcut::RangeCollector

Inherits:
Object
  • Object
show all
Defined in:
lib/pcut/range_collector.rb

Class Method Summary collapse

Class Method Details

.collect(array, range_index) ⇒ Object



8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
# File 'lib/pcut/range_collector.rb', line 8

def self.collect(array, range_index)
  unless array.is_a?(Array)
    raise ArgumentError, "#{array.to_s} is not an array"
  end
  unless range_index.is_a?(Pcut::RangeIndex)
    raise ArgumentError, "#{range_index.to_s} is not a range_index"
  end

  return [] if array.empty?
  array_index = range_index.index - 1

  if range_index.include_backward?
    array[0..array_index]
  elsif range_index.include_forward?
    array[array_index..-1]
  else
    value = array[array_index]
    value ? [value] : []
  end
end