Hi all
there is one thing concerning enumerations I really would like to file a
CR
about. But I guess it is necessary to open my ears and listen to your
POVs
before doing so. I believe that similar things are already done in lots
of
extension libraries like e.g. facet.
The most concerning thing is that there will probably be no consensus
about
the magic dot notation, I know this has been discussed before.
Here we go
Potential RCR, opinions wanted
The following is a very common pattern.
an_enum.map{ |ele| ele.a_method }
I believe that it is sufficiently common to be generalized.
There are two idioms I would like to see
an_enum.apply_to_all :a_method
and
an_enum.apply_to_all.a_method
the mutating version should also be available
an_enum.apply_to_all! :a_method
and
an_enum.apply_to_all!.a_method
Reference Implementation:
module Enumerable
def apply_to_all(method = nil)
return ApplicationProxy.new( self, :modify => false ) unless
method
map { |ele| ele.send method }
end #def apply_to_all(method = nil)
def apply_to_all!(method = nil)
return ApplicationProxy.new( self, :modify => true ) unless
method
map! { |ele| ele.send method }
end #def apply_to_all(method = nil)
class ApplicationProxy
def initialize( enumerable, modifications = {} )
@modify = modifications[ :modify ]
@target = enumerable
end
def method_missing name, *args, &blk
@modify ?
@target.map!{ |ele| ele.send( name, *args, &blk ) } :
@target.map{ |ele| ele.send( name, *args, &blk ) }
end
end #class ApplicationProxy
end #class Enumeration
Ty in advance
Robert