Module: Enumerable

Defined in:
lib/prohax/patches/enumerable.rb

Instance Method Summary collapse

Instance Method Details

#as_index_for(&value_function) ⇒ Object



9
10
11
12
13
14
# File 'lib/prohax/patches/enumerable.rb', line 9

def as_index_for &value_function
  inject({}) do |accum, elem|
    accum[elem] = value_function.call(elem)
    accum
  end
end

#grepv(re) ⇒ Object



33
34
35
# File 'lib/prohax/patches/enumerable.rb', line 33

def grepv re
  select { |s| s !~ re }
end

#group_by(&key_function) ⇒ Object



26
27
28
29
30
31
# File 'lib/prohax/patches/enumerable.rb', line 26

def group_by &key_function
  inject(Hash.new { |h,k| h[k] = [] }) do |hsh,elem|
    hsh[key_function.call(elem)] << elem
    hsh
  end
end

#index_by(&key_function) ⇒ Object



2
3
4
5
6
7
# File 'lib/prohax/patches/enumerable.rb', line 2

def index_by &key_function
  inject({}) do |accum, elem|
    accum[key_function.call(elem)] = elem
    accum
  end
end

#inject_with(sym) ⇒ Object



41
42
43
# File 'lib/prohax/patches/enumerable.rb', line 41

def inject_with sym
  inject { |a, b| a.send(sym, b) }
end

#take_while(&pred) ⇒ Object



16
17
18
19
20
21
22
23
24
# File 'lib/prohax/patches/enumerable.rb', line 16

def take_while &pred
  result = []
  each do |elem|
    if (!pred.call(elem))
      return result
    end
    result << elem
  end
end

#zip_with(arr, &blk) ⇒ Object



37
38
39
# File 'lib/prohax/patches/enumerable.rb', line 37

def zip_with arr, &blk
  zip(arr).map(&blk)
end