From: Devin Mullins Date: 2005-10-01T12:28:04+09:00 Subject: Lazy evaluaton (was Re: In your opinion....) gabriele renzi wrote: > Think for example of the IO monad: it allows you to state that you > should, say, print "hello" before "world". > The rest of the program is still happy, functional and lazy, since he > does'nt really care about the console output. The word "lazy", and the impression I was forming about monads from reading these posts, inspired this simple doodad. Is this useful? Is it already implemented in some other Ruby library out there? class Lazy (instance_methods - %w{__send__ __id__}).each {|meth| undef_method meth } def initialize(blk) @blk = blk end def method_missing(*a,&b) @inst = @blk.call unless defined? @inst @inst.send(*a,&b) end end def lazy(&blk) Lazy.new blk end a = lazy { puts "evaled!"; $some_expensive_db_operation or 5 } puts "Hello!" p a p a __END__ Output: Hello! evaled! 5 5 Devin