Class: Array

Inherits:
Object show all
Defined in:
parser/lib/uliska_parser/extensions.rb

Instance Method Summary (collapse)

Instance Method Details

- (Object) merge_by(array, column)

Merge array of hashes with similar array by one of the columns (if value for the key are the same). New rows in array2 ade added to self.



26
27
28
29
30
31
32
33
# File 'parser/lib/uliska_parser/extensions.rb', line 26

def merge_by array, column
  self.each do |row|
    if mrg = array.find { |x| x[column] == row[column] } rescue nil
      row.merge!(array.delete(mrg))
    end
  end
  (self + array)
end

- (Object) sum

Calculate sum of a numeric (or what looks like numeric) array. Array elements must be numeric or by respond by to_f.



14
15
16
# File 'parser/lib/uliska_parser/extensions.rb', line 14

def sum
  self.map(&:to_f).inject(0,:+)
end

- (Object) sum_by(key)

Find sum of column in array of hashes, by key in a hash



19
20
21
# File 'parser/lib/uliska_parser/extensions.rb', line 19

def sum_by key
  self.map { |x| x[key] }.sum
end