Created
March 17, 2010 13:39
-
-
Save severin/335227 to your computer and use it in GitHub Desktop.
Lazy Proxy
This file contains 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
class LazyProxy | |
# blank slate... (use BasicObject in Ruby 1.9) | |
instance_methods.each do |method| | |
undef_method(method) unless method =~ /^__/ | |
end | |
def initialize(&lazy_proxy_block) | |
@lazy_proxy_block = lazy_proxy_block | |
end | |
def method_missing(method, *args, &block) | |
@lazy_proxy_obj ||= @lazy_proxy_block.call # evaluate the real receiver | |
@lazy_proxy_obj.send(method, *args, &block) # delegate unknown methods to the real receiver | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment