lib/cotcube-helpers/array_ext.rb in cotcube-helpers-0.1.3 vs lib/cotcube-helpers/array_ext.rb in cotcube-helpers-0.1.4
- old
+ new
@@ -1,24 +1,27 @@
-class Array
+# frozen_string_literal: true
+# Monkey patching the Ruby Core class Array
+class Array
# returns nil if the compacted array is empty, otherwise returns the compacted array
- def compact_or_nil(*args, &block)
- return nil if self.compact == []
- yield self.compact
+ def compact_or_nil(*_args)
+ return nil if compact == []
+
+ yield compact
end
# sorts by a given attribute and then returns groups of where this attribute is equal
# .... seems like some_array.group_by(&attr).values
def split_by(attrib)
res = []
- sub = []
- self.sort_by(&attrib).each do |elem|
- if sub.empty? or sub.last[attrib] == elem[attrib]
+ sub = []
+ sort_by(&attrib).each do |elem|
+ if sub.empty? || (sub.last[attrib] == elem[attrib])
sub << elem
else
res << sub
- sub = [ elem ]
+ sub = [elem]
end
end
res << sub
res
end
@@ -35,19 +38,19 @@
block.call(self[i - 1], self[i])
end.compact
end
- alias_method :one_by_one, :pairwise
+ alias one_by_one pairwise
# same as pairwise, but with arity of three
def triplewise(&block)
raise ArgumentError, 'Array.triplewise needs an arity of 3 (i.e. |a, b, c|)' unless block.arity == 3
return [] if size <= 2
each_with_index.map do |_, i|
next if i < 2
- block.call(self[i - 2], self[i-1], self[i])
+ block.call(self[i - 2], self[i - 1], self[i])
end.compact
end
end