Module: Enumerable

Defined in:
lib/util.rb,
lib/util.rb

Instance Method Summary collapse

Instance Method Details

#argfindObject



26
27
28
29
# File 'lib/util.rb', line 26

def argfind
  each { |e| x = yield(e); return x if x }
  nil
end

#count_of(&b) ⇒ Object



6
# File 'lib/util.rb', line 6

def count_of(&b); select(&b).size end

#group_byObject



31
32
33
34
35
36
# File 'lib/util.rb', line 31

def group_by
  inject({}) do |groups, element|
    (groups[yield(element)] ||= []) << element
    groups
  end
end

#map_with_indexObject

sigh…



20
21
22
23
24
# File 'lib/util.rb', line 20

def map_with_index # sigh...
  ret = []
  each_with_index { |e, i| ret << yield(e, i) }
  ret
end

#max_of(&b) ⇒ Object



7
# File 'lib/util.rb', line 7

def max_of(&b); map(&b).max end

#min_of(&b) ⇒ Object



8
# File 'lib/util.rb', line 8

def min_of(&b); map(&b).min end