# String command

**URL:** <https://www.ruby-forum.com/t/string-command/94986>\
**Category:** Ruby\
**Created:** [January 11, 2006, 5:18am UTC](https://www.ruby-forum.com/t/string-command/94986 "2006-01-11T05:18:47Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![casper\_the\_ghost](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/58956e/32.png) [@casper\_the\_ghost](https://www.ruby-forum.com/u/casper_the_ghost)\
**Post date:** [January 11, 2006, 5:18am UTC](https://www.ruby-forum.com/t/string-command/94986/1 "2006-01-11T05:18:47Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![casper\_the\_ghost](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/58956e/32.png) [@casper\_the\_ghost](https://www.ruby-forum.com/u/casper_the_ghost)\
**Post date:** [January 11, 2006, 6:04am UTC](https://www.ruby-forum.com/t/string-command/94986/2 "2006-01-11T06:04:10Z")

</div>

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.
