Quoting Erik Veenstra <google / erikveen.dds.nl>:

>    instance_methods.each do |method_name|
>      unless ["__send__", "__id__"].include?(method_name)
>        class_eval <<-"EOF"
>          def #{method_name}(*parms, &block)
>            method_missing(:#{method_name}, *parms, &block)
>          end
>        EOF
>      end
>    end

I've seen a lot of different ways of writing this; my personal
favorite is:

 instance_methods.each { |m| undef_method m unless m =~ /^__/ }

(I think I picked this up from someone on ruby-talk)

>    def method_missing(method_name, *parms, &block)
>      @block.call.send(method_name, *parms, &block)
>    end

One thing to be careful of -- if the block can't successfully
replace all references to the LazyLoad instance with the result
object, you will see really bizzare behavior as
each new method call on the LazyLoad reruns the (potentially
expensive) computation, only to be routed to a completely different
object.

To get around that problem, you'd want to remember the block's
result after calling it the first time (as lazy.rb does), rather
than calling it multiple times.

>  class Branch
>    def initialize
>      @items     = LazyLoad.new{load; @items}
>      @snapshots = LazyLoad.new{load; @snapshots}
>    end
>
>    def load
>      @items     = {}
>      @snapshots = {}
>
>      # Fill @items and @snapshots...
>      # EXPENSIVE, TIME CONSUMING, MEMORY HUNGRY!
>    end
>  end

You can do this with lazy.rb too:

 class Branch
   def initialize
     @items     = promise{load; @items}
     @snapshots = promise{load; @snapshots}
   end

   def load
     @items     = {}
     @snapshots = {}

     # Fill @items and @snapshots...
     # EXPENSIVE, TIME CONSUMING, MEMORY HUNGRY!
   end
 end

Since you mentioned you were using this in production, you really
might want to look into using lazy.rb instead.

While discovering the issues for yourself can be a valuable learning
experience, I'm not sure you want to be doing that in production
code...

-mental