David Vallner
1/11/2006 5:02:00 AM
ruby talk wrote:
>Hello,
>Is there a way to make ruby interpret a string as a set of command?
>for example
>action="p this is test one"
>action.command
>action="for i in 1..4
>p this is test two
>end"
>action.command
>
>and have the out put be
>this is test one
>this is test two
>this is test two
>this is test two
>this is test two
>
>
>I am working with TK gui and want to create a general popup menu fuction,
>but i need to set the commands for differnt menus.
>
>
>
eval the strings. Mind you, the "p this is test one" is bad code, you
can only eval syntactically valid Ruby. Also, eval has certain security
implications, especially with user-supplied strings forming part of the
eval-ed code. If you want to make general popup menus, I'd personally
use a proc instead of a string to specify the action, because you might
run into passing around bindings with your strings real soon for
non-trivial event handlers, and jagged blades and boggy swamps await
that way. Or perhaps not.
David Vallner