Skip to content

Instantly share code, notes, and snippets.

@jhbabon
Created July 17, 2014 13:53
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jhbabon/0a990c2616750567a7ef to your computer and use it in GitHub Desktop.
Save jhbabon/0a990c2616750567a7ef to your computer and use it in GitHub Desktop.
Lazy Proxy implementation in ruby 2.1
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