Forum: Ruby string command

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
ruby talk (Guest)
on 2006-01-11 06:18
(Received via mailing list)
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.
David V. (Guest)
on 2006-01-11 07:04
(Received via mailing list)
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 topic is locked and can not be replied to.