Created
May 12, 2009 14:22
-
-
Save abriening/110513 to your computer and use it in GitHub Desktop.
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 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