String command

Is there a way to make ruby interpret a string as a set of command?
for example
action=“p this is test one”
action=“for i in 1…4
p this is test two

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
but i need to set the commands for differnt menus.

ruby talk wrote:

and have the out put be

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 V.

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs