Module: Enumerable

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

Instance Method Summary collapse

Instance Method Details

#argfindObject



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

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



27
28
29
30
31
32
# File 'lib/util.rb', line 27

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

#map_with_indexObject

sigh…



16
17
18
19
20
# File 'lib/util.rb', line 16

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