Skip to content

Instantly share code, notes, and snippets.

@severin
Created March 17, 2010 13:39
Show Gist options
  • Save severin/335227 to your computer and use it in GitHub Desktop.
Save severin/335227 to your computer and use it in GitHub Desktop.
Lazy Proxy
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