Module | Enumerable::Argumentable |
In: |
lib/standard/facets/enumargs.rb
|
This is a simple reimplementation of the core Enumerable module to allow its methods to take and pass-on arbitrary arguments to the underlying each call. This library uses Enumerator and scans Enumerable so it can alwasy stay in sync.
NOTE: Any Enumerable method with a negative arity cannot pass arguments due to ambiguity in the argument count. So the methods inject and zip do NOT work this way, but simply work as they do in Enumerable. However, The methods find and detect have been made modified to work by removing its rarely used optional parameter and providing instead an optional keyword parameter (:ifnone => …). Please keep these difference in mind.
Example
require 'enumargs' class T include Enumerable::Argumentable def initialize(arr) @arr = arr end def each(n) arr.each{ |e| yield(e+n) } end end t = T.new([1,2,3]) t.collect(4) #=> [5,6,7]
Make exception for find (a negative arity method) to accept keyword argument.
ObjectSpace.find(Class, :ifnone=>lambda{1}) { |e| ... } ObjectSpace.find(Class, :ifnone=>lambda{1}) { |e| ... }