Class: Array
- Inherits:
-
Object
- Object
- Array
- Defined in:
- lib/cotcube-helpers/array_ext.rb
Overview
Monkey patching the Ruby Core class Array
Instance Method Summary collapse
-
#compact_or_nil(*_args) {|compact| ... } ⇒ Object
returns nil if the compacted array is empty, otherwise returns the compacted array.
- #deep_dup ⇒ Object
- #elem_raises?(&block) ⇒ Boolean
-
#pairwise(ret = nil, empty: nil, &block) ⇒ Object
(also: #one_by_one)
This method iterates over an Array by calling the given block on all 2 consecutive elements it returns a Array of self.size - 1.
- #select_right_by(inclusive: false, exclusive: false, initial: [], &block) ⇒ Object
-
#select_within(ranges:, attr: nil, &block) ⇒ Object
selects all elements from array that fit in given ranges.
-
#split_by(attrib) ⇒ Object
sorts by a given attribute and then returns groups of where this attribute is equal .…
-
#triplewise(ret = nil, &block) ⇒ Object
same as pairwise, but with arity of three.
Instance Method Details
#compact_or_nil(*_args) {|compact| ... } ⇒ Object
returns nil if the compacted array is empty, otherwise returns the compacted array
6 7 8 9 10 |
# File 'lib/cotcube-helpers/array_ext.rb', line 6 def compact_or_nil(*_args) return nil if compact == [] yield compact end |
#deep_dup ⇒ Object
115 116 117 118 119 120 121 122 123 124 |
# File 'lib/cotcube-helpers/array_ext.rb', line 115 def deep_dup map do |el| case el when Hash, Array el.deep_dup else el.dup end end end |
#elem_raises?(&block) ⇒ Boolean
102 103 104 105 106 107 108 109 110 111 112 113 |
# File 'lib/cotcube-helpers/array_ext.rb', line 102 def elem_raises?(&block) raise ArgumentError, "Must provide a block." unless block_given? raise ArgumentError, "Block must have arity of 1." unless block.arity == 1 map do |elem| begin block.call(elem) false rescue elem end end.reject{|z| z.is_a? FalseClass }.tap{|z| z.empty? ? (return false) : (return z)} end |
#pairwise(ret = nil, empty: nil, &block) ⇒ Object Also known as: one_by_one
This method iterates over an Array by calling the given block on all 2 consecutive elements it returns a Array of self.size - 1
32 33 34 35 36 37 38 39 40 41 42 43 |
# File 'lib/cotcube-helpers/array_ext.rb', line 32 def pairwise(ret=nil, empty: nil, &block) raise ArgumentError, 'Array.one_by_one needs an arity of 2 (i.e. |a, b|)' unless block.arity == 2 raise ArgumentError, 'Each element of Array should respond to []=, at least the last one fails.' if not ret.nil? and not self.last.respond_to?(:[]=) return empty ||= [] if size <= 1 each_index.map do |i| next if i.zero? r = block.call(self[i - 1], self[i]) ret.nil? ? r : (self[i][ret] = r) end.compact end |
#select_right_by(inclusive: false, exclusive: false, initial: [], &block) ⇒ Object
82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 |
# File 'lib/cotcube-helpers/array_ext.rb', line 82 def select_right_by(inclusive: false, exclusive: false, initial: [], &block) # unless range.is_a? Range and # (range.begin.nil? or range.begin.is_a?(Integer)) and # (range.end.nil? or range.end.is_a?(Integer)) # raise ArgumentError, ":range, if given, must be a range of ( nil|Integer..nil|Integer), got '#{range}'" # end raise ArgumentError, 'No block given.' unless block.is_a? Proc inclusive = true unless exclusive if inclusive && exclusive raise ArgumentError, "Either :inclusive or :exclusive must remain falsey, got '#{inclusive}' and '#{exclusive}'" end index = find_index { |obj| block.call(obj) } self[((inclusive ? index : index + 1)..)] end |
#select_within(ranges:, attr: nil, &block) ⇒ Object
selects all elements from array that fit in given ranges. if :attr is given, selects all elements, where elem fit raises if elem.first.nil?
64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 |
# File 'lib/cotcube-helpers/array_ext.rb', line 64 def select_within(ranges:, attr: nil, &block) unless attr.nil? || first[attr] raise ArgumentError, "At least first element of Array '#{first}' does not contain attr '#{attr}'!" end raise ArgumentError, 'Ranges should be an Array or, more precisely, respond_to :map' unless ranges.respond_to? :map raise ArgumentError, 'Each range in :ranges should respond to .include!' unless ranges.map do |x| x.respond_to? :include? end.reduce(:&) select do |el| value = attr.nil? ? el : el[attr] ranges.map do |range| range.include?(block.nil? ? value : block.call(value)) end.reduce(:|) end end |
#split_by(attrib) ⇒ Object
sorts by a given attribute and then returns groups of where this attribute is equal .… seems like some_array.group_by(&attr).values
14 15 16 17 18 19 20 21 22 23 24 25 26 27 |
# File 'lib/cotcube-helpers/array_ext.rb', line 14 def split_by(attrib) res = [] sub = [] sort_by(&attrib).each do |elem| if sub.empty? || (sub.last[attrib] == elem[attrib]) sub << elem else res << sub sub = [elem] end end res << sub res end |
#triplewise(ret = nil, &block) ⇒ Object
same as pairwise, but with arity of three
48 49 50 51 52 53 54 55 56 57 58 59 |
# File 'lib/cotcube-helpers/array_ext.rb', line 48 def triplewise(ret=nil, &block) raise ArgumentError, 'Array.triplewise needs an arity of 3 (i.e. |a, b, c|)' unless block.arity == 3 raise ArgumentError, 'Each element of Array should respond to []=, at least the last one fails.' if not ret.nil? and not self.last.respond_to?(:[]=) return [] if size <= 2 each_index.map do |i| next if i < 2 r = block.call(self[i - 2], self[i - 1], self[i]) ret.nil? ? r : (self[i][ret] = r) end.compact end |