Created
September 25, 2009 17:25
-
-
Save jbr/193695 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
module Enumerable | |
def proxy(method) Proxy.new method, self end | |
class Proxy | |
instance_methods.each { |m| undef_method m unless m =~ /^__/ } | |
def initialize(method, enumerable) | |
@method, @enumerable = method, enumerable | |
end | |
def method_missing(method, *args) | |
@enumerable.send(@method) {|a| a.send method, *args} | |
end | |
end | |
end | |
class Array | |
%w(each map select reject all?).each do |method| | |
aliased_target, punctuation = method.to_s.sub(/([?!])$/, ''), $1 | |
with_proxy = "#{aliased_target}_with_proxy#{punctuation}" | |
without_proxy = with_proxy.sub "with", "without" | |
class_eval %{ | |
def #{with_proxy}(&blk) | |
blk.nil? ? proxy(:#{method}) : #{without_proxy}(&blk) | |
end | |
alias_method :#{without_proxy}, :#{method} | |
alias_method :#{method}, :#{with_proxy} | |
} | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment