Skip to content

Instantly share code, notes, and snippets.

@abriening
Created May 12, 2009 14:22
Show Gist options
  • Save abriening/110513 to your computer and use it in GitHub Desktop.
Save abriening/110513 to your computer and use it in GitHub Desktop.
class LazEvalProxy
alias_method :proxy_respond_to?, :respond_to?
alias_method :proxy_extend, :extend
instance_methods.each { |m| undef_method m unless m =~ /(^__|^proxy_)/ }
attr_accessor :proxy_block, :proxy_object
def initialize(object, block)
@proxy_object, @proxy_block = object, block
end
def method_missing(*args, &block)
unless defined?(@result)
@result = proxy_object.instance_eval &proxy_block
end
@result.send(*args, &block)
end
end
def Object.lazeval(&block)
LazEvalProxy.new self, block
end
def lazeval(&block)
LazEvalProxy.new self, block
end
# x = proc{|n| [n, lazeval{ x.call(n+1) } ] } # will system_stack_error in irb
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment