From: Brian Candler Date: 2011-05-20T00:06:32+09:00 Subject: Re: Generating Functions in Ruby Have a look at how ActionView (from Rails) caches its compiled templates. It does this by defining methods on a module. [actionpack/lib/action_view/template.rb] def compile(view, mod) #:nodoc: ... code = arity.abs == 1 ? @handler.call(self) : @handler.call(self, view) source = <<-end_src def #{method_name}(local_assigns) _old_output_buffer = @output_buffer;#{locals_code};#{code} ensure @output_buffer = _old_output_buffer end end_src ... begin mod.module_eval(source, identifier, 0) ObjectSpace.define_finalizer(self, Finalizer[method_name, mod]) rescue Exception => e # errors from template code if logger = (view && view.logger) logger.debug "ERROR: compiling #{method_name} RAISED #{e}" logger.debug "Function body: #{source}" logger.debug "Backtrace: #{e.backtrace.join("\n")}" end -- Posted via http://www.ruby-forum.com/.