Created
July 17, 2014 13:53
-
-
Save jhbabon/0a990c2616750567a7ef to your computer and use it in GitHub Desktop.
Lazy Proxy implementation in ruby 2.1
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 < BasicObject | |
def self.promise(&callback) | |
new(&callback) | |
end | |
def initialize(&callback) | |
@callback = callback | |
end | |
def method_missing(method, *args, &block) | |
__target__.send(method, *args, &block) | |
end | |
def __target__ | |
@target ||= @callback.call | |
end | |
end | |
module Kernel | |
def promise(&callback) | |
LazyProxy.promise(&callback) | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment