Sha256: 4abd548989a1a2ecb9908babae6153cc66d1ba6b1355ca29ed6fabd3ca538c6f

Contents?: true

Size: 694 Bytes

Versions: 1

Compression:

Stored size: 694 Bytes

Contents

class Array

  # http://madeofcode.com/posts/74-ruby-core-extension-array-sum
  def sum(method = nil)
    if block_given?
      raise ArgumentError, 'You cannot pass both a block and a method' if method
      inject(0) { |s, x| s + yield(x) }
    elsif method
      inject(0) { |s, x| s + x.send(method) }
    else
      inject(0) { |s, x| s + x }
    end
  end unless method_defined?(:sum)

  def mean(method = nil, &block)
    sum(method, &block) / size.to_f
  end unless method_defined?(:mean)

  def grouped
    group_by { |x| x }
  end unless method_defined?(:grouped)

  def group_by
    hash = {}
    each { |x| hash[yield(x)] = x }
    hash
  end unless method_defined?(:group_by)

end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
blackwinter-pru-0.1.4 lib/pru/core_ext/array.rb