Class: Array

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

Instance Method Summary collapse

Instance Method Details

#contain?(other) ⇒ Boolean

Returns:

  • (Boolean)


59
60
61
# File 'lib/ab_admin/core_ext/array.rb', line 59

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

#deep_merge_hashesObject



9
10
11
12
13
14
# File 'lib/ab_admin/core_ext/array.rb', line 9

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)


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

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

#map_val(attr = 'id') ⇒ Object



55
56
57
# File 'lib/ab_admin/core_ext/array.rb', line 55

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

#meanObject



16
17
18
19
# File 'lib/ab_admin/core_ext/array.rb', line 16

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

#pearson(b) ⇒ Object



33
34
35
36
# File 'lib/ab_admin/core_ext/array.rb', line 33

def pearson(b)
  return 0 if self.size != b.size || !Object.const_defined?("Statsample")
  Statsample::Bivariate.pearson(self.to_scale, b.to_scale)
end

#pluck!(method, *args) ⇒ Object Also known as: invoke!



38
39
40
# File 'lib/ab_admin/core_ext/array.rb', line 38

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

#safe_sdObject



25
26
27
28
29
30
31
# File 'lib/ab_admin/core_ext/array.rb', line 25

def safe_sd
  begin
    self.to_scale.sd
  rescue
    0.0
  end
end

#to_hashObject



67
68
69
70
71
# File 'lib/ab_admin/core_ext/array.rb', line 67

def to_hash
  h = {}
  each { |k, v| h[k] = v }
  h
end

#val_detect(attr, val) ⇒ Object



73
74
75
# File 'lib/ab_admin/core_ext/array.rb', line 73

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

#without(*values) ⇒ Object



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

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

#without!(*values) ⇒ Object



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

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

#word_countObject



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

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

#zip_allObject



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

def zip_all
  self[0].zip *self[1..-1]
end