Module: Hamachi::EnumerableExt

Defined in:
lib/hamachi/source/enumerable_ext.rb

Instance Method Summary collapse

Instance Method Details

#freqObject



18
19
20
21
22
23
24
25
26
# File 'lib/hamachi/source/enumerable_ext.rb', line 18

def freq
  h = Hash.new(0)
  if block_given?
    each { |each| h[yield each] += 1 }
  else
    each { |each| h[each] += 1 }
  end
  h.sort_by(&:last).to_h
end

#index_byObject



10
11
12
13
14
15
16
# File 'lib/hamachi/source/enumerable_ext.rb', line 10

def index_by
  raise unless block_given?

  index = Hash.new
  self.each { |each| index[yield each] = each }
  index
end

#where(patterns) ⇒ Object



28
29
30
# File 'lib/hamachi/source/enumerable_ext.rb', line 28

def where(patterns)
  each.select { |each| patterns.all? { |symbol, pattern| pattern === each.send(symbol) }}
end

#wherent(patterns) ⇒ Object



32
33
34
# File 'lib/hamachi/source/enumerable_ext.rb', line 32

def wherent(patterns)
  each.reject { |each| patterns.all? { |symbol, pattern| pattern === each.send(symbol) }}
end