module Enumerable def Enumerable.inject_with(object, sym, id, *args) object.send(sym, *args) {|*values| id = yield id, *values } id end end
-- David Black
object.to_enum(method).inject(init){....}
Using enumerator, you can enumerate over any method, not just inject.
--matz.
For what it can do - this is what I used it for:
ifile, idir = *[:file?, :directory?].map {|sym|
Proc.new {|root| inject_with(Find, :find, [], root) {|arr, path|
FileTest.send(sym, path) ? arr << path : arr
}}
}-- i.e. make procs to find all files and all directories using find. (simpler way to do that?) but it just seemed like a nice add on - but apparently enumerator can work. Is there documentation for enumerator somewhere in English?
You can find enumerator.txt in the ext/enumerator directory in the distribution archive.
--matz.
Back to RCRchive.
RCR Submission page and RCRchive powered by Ruby, Apache, RuWiki (modified), and RubLog