2

Say I have the code:

print(">>> ")
command = gets

What I want to do, is execute the command variable as one would normally do in Ruby, by putting backticks around the code you want to execute. How would I do this? If I do:

`command`

it just tries to execute the word command. How do I execute the variable???

2
  • 3
    Be very careful doing this. Allowing a user to enter a command, then executing it for them, allows them to execute the command with the permissions your code runs under. If it runs as root, and the user enters "rm -rf /", your system will go bye-bye really fast. Commented Apr 3, 2012 at 21:55
  • yep i know! I'm obviously not gonna release this to other people, I just got the idea from the book Metaprogramming Ruby. About a page after, it told me how bad of an idea it was haha Commented Apr 4, 2012 at 1:33

1 Answer 1

5

Just interpolate it like you would with a string:

`#{command}`
Sign up to request clarification or add additional context in comments.

1 Comment

+1 You can guess things like this in Ruby and surprisingly often it just works.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.