# String to code?

**URL:** <https://www.ruby-forum.com/t/string-to-code/123291>\
**Category:** Ruby\
**Created:** [December 16, 2007, 12:48am UTC](https://www.ruby-forum.com/t/string-to-code/123291 "2007-12-16T00:48:55Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![pavel989](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/48db29/32.png) [@pavel989](https://www.ruby-forum.com/u/pavel989)\
**Post date:** [December 16, 2007, 12:48am UTC](https://www.ruby-forum.com/t/string-to-code/123291/1 "2007-12-16T00:48:55Z")

</div>

is it possible to get user input, and put that in as a code. like lets  
say i ask for command and the user types types something like twice 3  
which would refer to to a method twice n??

---

<div class="post-metadata">

**Author:** ![pavel989](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/48db29/32.png) [@pavel989](https://www.ruby-forum.com/u/pavel989)\
**Post date:** [December 16, 2007, 1:00am UTC](https://www.ruby-forum.com/t/string-to-code/123291/2 "2007-12-16T01:00:51Z")

</div>

Pavel P. wrote:

> is it possible to get user input, and put that in as a code. like lets  
> say i ask for command and the user types types something like twice 3  
> which would refer to to a method twice n??

Well, you can certainly use the eval method, which just executes a  
string as Ruby code, but there is some risk involved. You probably  
wouldn’t want to allow somebody to type

system(‘del _._’)

for example.

Unless you’re very smart and very careful (like \_why:  
[http://tryruby.hobix.com/](http://tryruby.hobix.com/)) you shouldn’t allow the user to type in real  
Ruby code.

---

<div class="post-metadata">

**Author:** ![pavel989](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/48db29/32.png) [@pavel989](https://www.ruby-forum.com/u/pavel989)\
**Post date:** [December 16, 2007, 6:11am UTC](https://www.ruby-forum.com/t/string-to-code/123291/3 "2007-12-16T06:11:43Z")

</div>

yes i know that, and in the actual app im not going to have that. just  
for my self.

thank you very much

I love ruby

---

<div class="post-metadata">

**Author:** ![pavel989](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/48db29/32.png) [@pavel989](https://www.ruby-forum.com/u/pavel989)\
**Post date:** [December 16, 2007, 10:23am UTC](https://www.ruby-forum.com/t/string-to-code/123291/4 "2007-12-16T10:23:42Z")

</div>

Tim H. wrote:

> Well, you can certainly use the eval method, which just executes a  
> string as Ruby code, but there is some risk involved. You probably  
> wouldn’t want to allow somebody to type
> 
> system(‘del _._’)
> 
> for example.

Why not? I mean the user wouldn’t type something like this without  
knowing  
what it means/does. So if he wants to delete all his files why not let  
him?

Of course it’s different when the script runs on another system than the  
user  
is on, but since the op mentioned getting input from the user via gets,  
I  
assume this is not the case here.
