[Coldstuff] evaluate method
Brandon Gillespie
coldstuff@cold.org
Mon, 14 Jan 2002 22:15:45 -0700
On Mon, Jan 14, 2002 at 11:04:57PM -0500, Adam Cormany wrote:
> I've been trying and can't figure out how to do this on my own so hopefully the cold gods will smile on me tonight:)
>
> I'm parsing some text and by the time it's all finished, I have a string of an equation of numbers. Is there a way to process this string to get the answer? For example, I have a string "1+1", is there a way to process this to get 2? I know about @eval or ; and I want to do exactly that but inside some code as a method. I've been looking at $programmer.eval_cmd and $programmer.evaluate but I'm lost. Am I missing something or is there a simple way to do <variable> = .<method>("1+1") ?
>
@eval and ; on coldcore use .evaluate(), but there is a distilled
version as .eval which you can use. The only gotcha is it expects a
full method, so include complete statements with 'return'. For your
case, do:
variable = .eval(["return 1+1"])[2];
Or you can do what .eval is doing, and do your own .add_method, exec,
del_method.
-Brandon