From: Ken Bloom Date: 2008-04-04T22:40:16+09:00 Subject: Re: Using a string as executable code On Thu, 03 Apr 2008 20:42:28 -0500, Paul McMahon wrote: > Peter Marks wrote: >> I need to store one line method calls as strings in my db and then >> apply them to an object. For example, I would store "name.upcase" in >> the db and then somehow call person.name.upcase. Any idea how I might >> pull this off? > > Well, you could do > > result = "name.upcase".split(".").inject(person) do |object, method| > object.send(method) > end > > but storing code in a database smells. Perhaps it would be better to > find another approach that avoids storing the code in the first place Your answer also smells. I'd use instance_eval. result = person.instance_eval "name.upcase" --Ken -- Ken (Chanoch) Bloom. PhD candidate. Linguistic Cognition Laboratory. Department of Computer Science. Illinois Institute of Technology. http://www.iit.edu/~kbloom1/