Sha256: 61dbaa5ff9b4808391b67b16c8df99eab5a6850efe3ad18bb318fa2f01d6abdc

Contents?: true

Size: 1.18 KB

Versions: 10

Compression:

Stored size: 1.18 KB

Contents

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

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

  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

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

  #def zip_all
  #  self[0].zip *self[1..-1]
  #end
  #
  #def pluck!(method, *args)
  #  each_index { |x| self[x] = self[x].send method, *args }
  #end
  #
  #alias invoke! pluck!

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

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

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

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

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

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

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

end

#module Enumerable
#  def pluck(method, *args)
#    map { |x| x.send method, *args }
#  end
#
#  alias invoke pluck
#end

Version data entries

10 entries across 10 versions & 1 rubygems

Version Path
ab_admin-0.4.0 lib/ab_admin/core_ext/array.rb
ab_admin-0.3.6 lib/ab_admin/core_ext/array.rb
ab_admin-0.3.5 lib/ab_admin/core_ext/array.rb
ab_admin-0.3.4 lib/ab_admin/core_ext/array.rb
ab_admin-0.3.3 lib/ab_admin/core_ext/array.rb
ab_admin-0.3.2 lib/ab_admin/core_ext/array.rb
ab_admin-0.3.1 lib/ab_admin/core_ext/array.rb
ab_admin-0.3.0 lib/ab_admin/core_ext/array.rb
ab_admin-0.2.3 lib/ab_admin/core_ext/array.rb
ab_admin-0.2.2 lib/ab_admin/core_ext/array.rb