From: Trans Date: 2005-11-05T14:37:10+09:00 Subject: Re: lazy.rb Pit Capitain wrote: > MenTaLguY schrieb: > > ... > > Same problem in both cases, basically. > > Here's another implementation that doesn't have these problems: > > module Lazy #:nodoc: all > class Thunk > instance_methods.each { |m| undef_method m unless m =~ /^__/ } > def initialize( &computation ) > @computation = computation > end > def method_missing( *args, &block ) > ( @result ||= @computation.call( self ) ).send( *args, &block ) > end > end > end Ah, that clarifies things. This is essentially a pass-thru Functor with a cached result. (Why is 'self' being passed to the computation though?) While not quite the same as the implict lambda's I suggested, it is interesting just how similar they are. This whole Functor paradigm, including things like #every, #enum_for, #as (was #superfunc), etc. and now this --all are various simple decorators of a special form where the heart of code is some sort of transfomation in method_missing. Insteresting stuff. It would be even more interesting to see if this could somehow be embodied in core so to operate more efficiently (and perhaps a syntax variation on the methods?) --this techinque may have potential for roles and namespace selectors too. T.