From: Brian Candler Date: 2002-10-17T06:20:04+09:00 Subject: Precompiling eval expressions What ways are there to pre-compile an "eval" expression which is going to be used multiple times, so that it does not have to be parsed every time? This would be similar to how a Regexp object can be used repeatedly. So far the best I can come up with is to eval Proc.new, although the resulting object is only able to access objects which exist at the time of definition: myexpr = '"The time is #{t}"' myproc = eval "Proc.new { #{myexpr} }" t = 123 myproc.call => NameError: undefined local variable or method `t' for # But if it did exist at that time, it _is_ able to access the 'current' value, not the value at definition time: t = 0 myexpr = '"The time is #{t}"' myproc = eval "Proc.new { #{myexpr} }" t = 123 myproc.call => "The time is 123" t = 456 myproc.call => "The time is 456" (Can anyone point me to a good explanation of what is going on underneath?) Anyway, the idea is that I want to load in a number of expressions from a database (for example containing rules for validating fields or providing default values); those rules may be used many times but only read in once. Precompiling them also gives an opportunity to catch syntax errors before they are actually used. Regards, Brian.