On Jul 10, 2005, at 2:25 PM, Robin Stocker wrote:

> Here's my solution...

Here's what I came up with while building the quiz:

class SerializableProc
     def self._load( proc_string )
         new(proc_string)
     end

     def initialize( proc_string )
         @code = proc_string
         @proc = nil
     end

     def _dump( depth )
         @code
     end

     def method_missing( method, *args )
         if to_proc.respond_to? method
             @proc.send(method, *args)
         else
             super
         end
     end

     def to_proc(  )
         return @proc unless @proc.nil?

         if @code =~ /\A\s*(?:lambda|proc)(?:\s*\{|\s+do).*(?:\}|end) 
\s*\Z/
             @proc = eval @code
         elsif @code =~ /\A\s*(?:\{|do).*(?:\}|end)\s*\Z/
             @proc = eval "lambda #{@code}"
         else
             @proc = eval "lambda { #{@code} }"
         end
     end

     def to_yaml(  )
         @proc = nil
         super
     end
end

> (Question: Is it better if I attach it or just paste it like this?)

It doesn't much matter, but I favor inlining it when it's a single file.

James Edward Gray II