Sha256: c63e5a248530bc69189eda3b72d49a15ed038978e2d5607380c42cca1ef214f1

Contents?: true

Size: 757 Bytes

Versions: 34

Compression:

Stored size: 757 Bytes

Contents

class Array
  def reduce(initial_value = 0, &proc)
    calculated_value = initial_value
    self.each_with_index do |e, i|
      calculated_value = yield(calculated_value, e, size, i)
    end
    calculated_value
  end
  
  def to_freq_hash
    freq = {}
    each do |e|
      freq[e] = freq[e].to_i + 1
    end
    freq
  end
  
  def to_summarized_hash(operation_symbol = :+)
    sums = {}
    each do |e|
      key, value = yield(e)
      if sums[key]
        sums[key] = sums[key].send(operation_symbol, value)
      else
        sums[key] = value
      end
    end
    sums
  end
  
  def to_grouped_hash
    groups = {}
    each do |e|
      key, value = yield(e)
      groups[key] ||= []
      groups[key] << value
    end
    groups
  end
  
end
  

Version data entries

34 entries across 34 versions & 1 rubygems

Version Path
backlog-0.36.2 lib/array_helper.rb
backlog-0.17.6 lib/array_helper.rb
backlog-0.18.0 lib/array_helper.rb
backlog-0.19.0 lib/array_helper.rb
backlog-0.20.0 lib/array_helper.rb
backlog-0.20.1 lib/array_helper.rb
backlog-0.21.0 lib/array_helper.rb
backlog-0.21.1 lib/array_helper.rb
backlog-0.21.2 lib/array_helper.rb
backlog-0.21.3 lib/array_helper.rb
backlog-0.22.1 lib/array_helper.rb
backlog-0.22.0 lib/array_helper.rb
backlog-0.23.0 lib/array_helper.rb
backlog-0.24.0 lib/array_helper.rb
backlog-0.23.1 lib/array_helper.rb
backlog-0.25.0 lib/array_helper.rb
backlog-0.26.0 lib/array_helper.rb
backlog-0.28.0 lib/array_helper.rb
backlog-0.29.0 lib/array_helper.rb
backlog-0.30.0 lib/array_helper.rb