Module: RorHack::ArrayHack
- Included in:
- Array
- Defined in:
- lib/ror_hack/array_hack.rb
Instance Method Summary collapse
- #all_blank? ⇒ Boolean
- #all_present? ⇒ Boolean
- #any_blank? ⇒ Boolean
- #any_present? ⇒ Boolean
- #include_all?(*args) ⇒ Boolean
- #include_any?(*args) ⇒ Boolean
- #left_push(num, ele = nil) ⇒ Object
-
#left_strip_nil ⇒ Object
清空左边为nil的所有元素.
- #multi_group_by(*keys) ⇒ Object
- #right_push(num, ele = nil) ⇒ Object
- #right_strip_nil ⇒ Object
- #strip_nil ⇒ Object
Instance Method Details
#all_blank? ⇒ Boolean
30 31 32 |
# File 'lib/ror_hack/array_hack.rb', line 30 def all_blank? self.all?(&:blank?) end |
#all_present? ⇒ Boolean
38 39 40 |
# File 'lib/ror_hack/array_hack.rb', line 38 def all_present? self.all?(&:present?) end |
#any_blank? ⇒ Boolean
34 35 36 |
# File 'lib/ror_hack/array_hack.rb', line 34 def any_blank? self.any?(&:blank?) end |
#any_present? ⇒ Boolean
42 43 44 |
# File 'lib/ror_hack/array_hack.rb', line 42 def any_present? self.any?(&:present?) end |
#include_all?(*args) ⇒ Boolean
26 27 28 |
# File 'lib/ror_hack/array_hack.rb', line 26 def include_all?(*args) args.all? { |i| i.in? self } end |
#include_any?(*args) ⇒ Boolean
22 23 24 |
# File 'lib/ror_hack/array_hack.rb', line 22 def include_any?(*args) args.any? { |i| i.in? self } end |
#left_push(num, ele = nil) ⇒ Object
70 71 72 73 |
# File 'lib/ror_hack/array_hack.rb', line 70 def left_push(num, ele = nil) unshift(ele) while size < num self end |
#left_strip_nil ⇒ Object
清空左边为nil的所有元素.
53 54 55 56 57 58 59 |
# File 'lib/ror_hack/array_hack.rb', line 53 def left_strip_nil while first.nil? delete_at(0) break if size == 0 end self end |
#multi_group_by(*keys) ⇒ Object
3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 |
# File 'lib/ror_hack/array_hack.rb', line 3 def multi_group_by(*keys) group_by_block = lambda do |array, ks| key = ks.shift return array unless key if key.is_a? Proc hash = array.group_by(&key) else hash = array.group_by { |i| i[key] } end if ks.present? hash.keys.each do |k| hash[k] = group_by_block.call hash[k], ks.dup end end return hash end group_by_block.call self, keys end |
#right_push(num, ele = nil) ⇒ Object
65 66 67 68 |
# File 'lib/ror_hack/array_hack.rb', line 65 def right_push(num, ele = nil) push(ele) while size < num self end |
#right_strip_nil ⇒ Object
46 47 48 49 50 |
# File 'lib/ror_hack/array_hack.rb', line 46 def right_strip_nil self.reverse! left_strip_nil reverse end |
#strip_nil ⇒ Object
61 62 63 |
# File 'lib/ror_hack/array_hack.rb', line 61 def strip_nil left_strip_nil.right_strip_nil end |