Class: Array

Inherits:
Object show all
Defined in:
lib/ab_admin/core_ext/array.rb

Instance Method Summary collapse

Instance Method Details

#add_or_delete(el) ⇒ Object



2
3
4
# File 'lib/ab_admin/core_ext/array.rb', line 2

def add_or_delete(el)
  include?(el) ? delete(el) : push(el)
end

#contain?(other) ⇒ Boolean

Returns:

  • (Boolean)


49
50
51
# File 'lib/ab_admin/core_ext/array.rb', line 49

def contain?(other)
  (other - self).empty?
end

#deep_merge_hashesObject



13
14
15
16
17
18
# File 'lib/ab_admin/core_ext/array.rb', line 13

def deep_merge_hashes
  self.inject({}) do |res, h|
    raise Exception.new("Not a hash #{h}") unless h.is_a?(Hash)
    h.deep_merge(res)
  end
end

#intersect?(other) ⇒ Boolean

Returns:

  • (Boolean)


53
54
55
# File 'lib/ab_admin/core_ext/array.rb', line 53

def intersect?(other)
  !(self & other).empty?
end

#map_val(attr = 'id') ⇒ Object



45
46
47
# File 'lib/ab_admin/core_ext/array.rb', line 45

def map_val(attr='id')
  map{|el| el[attr] }
end

#meanObject



20
21
22
23
# File 'lib/ab_admin/core_ext/array.rb', line 20

def mean
  return 0 if size == 0
  inject(:+) / size
end

#val_detect(attr, val) ⇒ Object

def to_hash ActiveSupport::Deprecation.warn('Array#to_hash is deprecated, use Array#to_h or Hash instead') h = {} each { |k, v| h = v } h end



64
65
66
# File 'lib/ab_admin/core_ext/array.rb', line 64

def val_detect(attr, val)
  detect{|v| v[attr] == val }
end

#without(*values) ⇒ Object

def zip_all self.zip *self end

def pluck!(method, *args) each_index { |x| self = self.send method, *args } end

alias invoke! pluck!



35
36
37
38
# File 'lib/ab_admin/core_ext/array.rb', line 35

def without(*values)
  copy = self.dup
  copy.without!(*values)
end

#without!(*values) ⇒ Object



40
41
42
43
# File 'lib/ab_admin/core_ext/array.rb', line 40

def without!(*values)
  values.flatten.each { |value| self.delete(value) }
  self
end

#word_countObject



6
7
8
9
10
11
# File 'lib/ab_admin/core_ext/array.rb', line 6

def word_count
  each_with_object({}) do |word, h|
    h[word] ||= 0
    h[word] += 1
  end
end