From: "ara.t.howard" Date: 2008-04-05T01:58:49+09:00 Subject: Re: Using a string as executable code On Apr 4, 2008, at 10:37 AM, Peter Marks wrote: > realize storing code in a database is kind of a funny idea, but I > don't see a better alternative for the problem I face. I need to > generate an array of object customized strings, specially selected > from > a pool of about 500 string functions. The only alternative I see is > defining about 300 extra frivolous string methods in my model and > selecting among those based on database values. That doesn't seem any > less smelly. there are many easy ways to handle this i don't know what you mean by 'string function' but: cfp:~ > cat a.rb module StringMethods class << self def a p :a end def b p :b end def c p :c end end end class Module def string_methods *list list.flatten.each do |m| module_eval <<-code def #{ m }(*a, &b) StringMethods.#{ m }(*a, &b) end code end end end class Model string_methods %w( a b ) end m = Model.new m.a m.b m.c cfp:~ > ruby a.rb :a :b a.rb:38: undefined method `c' for # (NoMethodError) a @ http://codeforpeople.com/ -- we can deny everything, except that we have the possibility of being better. simply reflect on that. h.h. the 14th dalai lama