Module: Enumerable

Defined in:
lib/charlie/1.9fixes.rb,
lib/charlie/etc/monkey.rb

Instance Method Summary collapse

Instance Method Details

#countObject



17
18
19
20
21
# File 'lib/charlie/etc/monkey.rb', line 17

def count
  count = 0
  each {|e| count+=1 if yield(e) }
  count
end

#group_byObject



11
12
13
14
15
# File 'lib/charlie/etc/monkey.rb', line 11

def group_by
  ret = {}
  each{|e| (ret[yield(e)] ||= []) << e }
  ret
end

#sumObject

faster than both r=0; each; r and {|a,b|a+b}



38
39
40
# File 'lib/charlie/1.9fixes.rb', line 38

def sum # faster than both r=0; each; r and {|a,b|a+b}
  inject(0,:+)
end

#zip_with(a2, &b) ⇒ Object

avoid Enumerable#zip in 1.9



43
44
45
# File 'lib/charlie/1.9fixes.rb', line 43

def zip_with(a2) # avoid Enumerable#zip in 1.9
  r=[]; each_with_index{|e,i| r << yield(e,a2[i]) }; r
end