lib/red_amber/helper.rb in red_amber-0.2.1 vs lib/red_amber/helper.rb in red_amber-0.2.2

- old
+ new

@@ -15,47 +15,49 @@ def integers?(enum) enum.all?(Integer) end - def sym_or_str?(enum) - enum.all? { |e| e.is_a?(Symbol) || e.is_a?(String) } - end - def booleans?(enum) enum.all? { |e| e.is_a?(TrueClass) || e.is_a?(FalseClass) || e.is_a?(NilClass) } end def create_dataframe_from_vector(key, vector) DataFrame.new(key => vector.data) end - def parse_to_vector(args) + def parse_to_vector(args, vsize: size) a = args.reduce([]) do |accum, elem| - accum.concat(normalize_element(elem)) + accum.concat(normalize_element(elem, vsize: vsize)) end Vector.new(a) end - def normalize_element(elem) + def normalize_element(elem, vsize: size) case elem - when Numeric, String, Symbol, TrueClass, FalseClass, NilClass - [elem] + when NilClass + [nil] when Range - both_end = [elem.begin, elem.end] - both_end[1] -= 1 if elem.exclude_end? && elem.end.is_a?(Integer) - - if both_end.any?(Integer) || both_end.all?(&:nil?) - if both_end.any? { |e| e&.>=(size) || e&.<(-size) } - raise DataFrameArgumentError, "Index out of range: #{elem} for 0..#{size - 1}" + bg = elem.begin + en = elem.end + if [bg, en].any?(Integer) + bg += vsize if bg&.negative? + en += vsize if en&.negative? + en -= 1 if en.is_a?(Integer) && elem.exclude_end? + if bg&.negative? || (en && en >= vsize) + raise DataFrameArgumentError, "Index out of range: #{elem} for 0..#{vsize - 1}" end - (0...size).to_a[elem] + Array(0...vsize)[elem] + elsif bg.nil? && en.nil? + Array(0...vsize) else - elem.to_a + Array[elem] end + when Enumerator + elem.to_a else - Array(elem) + Array[elem] end end end end